js 格式化时间日期

Date.prototype.format = function(format){
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
} 

if(/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
} 

for(var k in o) {
if(new RegExp("("+ k +")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
}
}
return format;
} 

//使用方法
var now = new Date();
var nowStr = now.format("yyyy-MM-dd hh:mm:ss");
//使用方法2:
var testDate = new Date();
var testStr = testDate.format("YYYY年MM月dd日hh小时mm分ss秒");
alert(testStr);
//示例:
alert(new Date().Format("yyyy年MM月dd日"));
alert(new Date().Format("MM/dd/yyyy"));
alert(new Date().Format("yyyyMMdd"));
alert(new Date().Format("yyyy-MM-dd hh:mm:ss"));

摘自:http://www.jb51.net/article/22657.htm

复制去Google翻译翻译结果

时间: 2024-07-29 02:32:43

js 格式化时间日期的相关文章

js 格式化时间日期函数小结

下面是脚本之家为大家整理的一些格式化时间日期的函数代码,需要的朋友可以参考下. 代码如下: Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(

php Smarty date_format [格式化时间日期]

Example 5-8. date_format[日期格式] index.php: 复制代码代码如下: $smarty = new Smarty; $smarty->assign('yesterday', strtotime('-1 day')); $smarty->display('index.tpl'); index.tpl: {$smarty.now|date_format} {$smarty.now|date_format:"%A, %B %e, %Y"} {$sm

js中时间日期的处理

// 增加天 function AddDays(date,value)  {     date.setDate(date.getDate()+value);  }   // 增加月 function AddMonths(date,value)  {     date.setMonth(date.getMonth()+value);  }   // 增加年 function AddYears(date,value)  {     date.setFullYear(date.getFullYear(

一篇文章吃透iOS、JS的时间日期(Date, Calendar, Locale, TimeZone)

iOS 时间相关类 NSDate - 表示一个绝对的时间点. NSCalendar - 代表一个特定的日历,例如公历或者希伯来日历.它提供了一系列基于日期的计算,并且可以让你在"NSDate"和"NSDateComponents"对象之间进行转换. NSDateComponents - 允许你获取一个Date的特定内容,例如小时.分钟.年月日等等. NSTimeZone - 代表一个特定的时区信息,可以帮助跨时区的计算任务. 代码分析 废话少说,Show me th

js 格式化时间/Date(1425027069000)/格式化为yyyy-MM-dd HH:mm:ss

//格式化时间[参数是一个字符串,格式类似于 /Date(1425027069000)/ ] function formatDate(n) { var s = n.slice(6, 21); dt = new Date(parseInt(n.slice(6, 21))); var year = dt.getFullYear();; var month = dt.getMonth() + 1; var date = dt.getDate(); var hour = dt.getHours(); v

js格式化时间

Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "H+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds()

js 格式化string日期格式

<script type="text/javascript"> /** * string 转时间格式; */ function formatDate(date, format) { if (date.length==0) return; if (!format) format = "yyyy-MM-dd"; switch(typeof date) { case "string": date = new Date(date.replac

JS格式化时间(支持小程序,兼容IOS)

const REGEX = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/export const formatTime = (val, format)=>{ if (val) { /** * @instructions 如果不是时间戳格式,且含有字符 '-' 则将 '-' 替换成 '/' && 删除小数点及后面的数字 * @reason 将 '-' 替换成 '/' && 删除小数点及后面的数字 的原因是safari

js 格式化时间毫秒

//格式华日期为2017-12-20 function datefomate(value) { if(value==null || value == undefined){ return ""; } var date = new Date(value); Y = date.getFullYear(), m = date.getMonth()+1, d = date.getDate(), H = date.getHours(), i = date.getMinutes(), s = da