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



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 getDategetMonth + 1getFullYeargetHours 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"






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值