您的位置:首页 > Web前端 > JQuery

How to format a JavaScript date(jquery ,json ,日期)

2015-11-11 19:07 911 查看

http://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date方法1:
var monthNames = [
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
];

var date = new Date();
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();

console.log(day, monthNames[monthIndex], year);
document.write(day + ' ' + monthNames[monthIndex] + ' ' + year);

执行结果:11 November 2015方法二:If you need to quickly format your date using plain JavaScript, use
getDate
,
getMonth
+ 1
,
getFullYear
,
getHours
and
getMinutes
:
var d = new Date();

var datestring = d.getDate()  + "-" + (d.getMonth()+1) + "-" + d.getFullYear() + " " +
d.getHours() + ":" + d.getMinutes();

// 16-5-2015 9:50
Or,
if you need it to be padded with zeros:
<span style="font-family:FangSong_GB2312;">var datestring = ("0" + d.getDate()).slice(-2) + "-" + ("0"+(d.getMonth()+1)).slice(-2) + "-" +
d.getFullYear() + " " + ("0" + d.getHours()).slice(-2) + ":" + ("0" + d.getMinutes()).slice(-2);

// 16-05-2015 09:50</span>
方法三:use the non-standard Date method
toLocaleFormat(formatString)
var today = new Date();
today.toLocaleFormat('%d-%b-%Y'); // 30-Dec-2011

注: only work in Firefox. Both IE and Chrome are failing方法四:If you are already using jQuery UI in your project you could do it this way:
var formatted = $.datepicker.formatDate("M d, yy", new Date("2014-07-08T09:02:21.377"));

// formatted will be 'Jul 8, 2014'
注:https://jqueryui.com/resources/demos/datepicker/date-formats.html方法五:use the date format library 路径(http://blog.stevenlevithan.com/archives/date-time-format)
var dateFormat = require('dateformat');
var today = new Date();
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
returns:

Saturday, June 9th, 2007, 5:46:21 PM
注:可以使用的JS工具类信息.1)http://momentjs.com/2)http://www.datejs.com/2007/11/27/getting-started-with-datejs/3)href="http://www.javascriptsource.com/repository/javascripts/2009/03/880961/JS_Simple_Date_Format.zip"

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: