jquery里,怎么格式化时间

2025-02-24 11:00:22
推荐回答(4个)
回答1:

jquery里格式化时间采用日期format函数:
1、需要格式化的时间html代码:
06-Aug-2012
2012/06/Aug
Monday, August 06, 2012
2、formt日期的核心js代码:
$(document).ready(function () {
$('span.date').each(function() {
var dateFormat = $(this).text()
var dateFormat = $.datepicker.formatDate('MM dd, yy', new Date(dateFormat));
//alert(dateFormat);
$(this).html(dateFormat + "
");
});
});
3、运行结果:
06-Aug-2012 2012/06/Aug Monday, August 06, 2012

回答2:

Date.setTime(0); 不需要JQ, js就行了。
Date.toLocaleDateString() 将日期对象转换成本地日期字符串格式

回答3:

/**
* 时间对象的格式化;
*/
Date.prototype.format = function(format) {
/*
* 使用例子:format="yyyy-MM-dd hh:mm:ss";
*/
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;
}

回答4:

从哪样格式到哪样??