简体   繁体   English

格式化日期“M d,yy”

[英]Format date in “M d, yy”

I want to format newDate in format Dec 24, 2013 which is currently like Tue Dec 24 2013 00:00:00 GMT+0530 我想格式化newDate格式为Dec 24, 2013 ,目前Tue Dec 24 2013 00:00:00 GMT+0530

var dateString = 'Dec 17, 2013'; // date string
var actualDate = new Date(dateString); // convert to actual date
var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()+7);

alert(dateString);

alert(newDate);

Fiddle: http://jsfiddle.net/7eRXh/1/ 小提琴: http//jsfiddle.net/7eRXh/1/

Hope this might help you. 希望这可能对你有所帮助。 Further more refer this document 进一步参考本文档

$.datepicker.formatDate('M d, yy', 
new Date(),
{ monthNamesShort: ['Jan','Feb','Mar','Apr','Maj','Jun','Jul',
                  'Aug','Sep','Okt','Nov','Dec'] 
});

使用JQuery日期格式插件,或者如果您有更多日期来处理项目date.js是正确的选择。

In pure Javascript , 在纯Javascript

There is no three digit format for default date in Javascript. Javascript中的默认日期没有三位数格式。 So create an array like 所以创建一个类似的数组

var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

Once your is date is parsed, pass the newDate values as below 解析newDate日期后,传递newDate值,如下所示

var con = monthNames[newDate.getMonth()] + " " + newDate.getDate() + ", " +   
                                                 newDate.getFullYear();
//returns Dec 24, 2013 

JSFiddle 的jsfiddle

With the help of phpjs you can atchieve this. 在phpjs的帮助下,你可以做到这一点。 Refer below url 请参阅以下网址

Phpjs date function Phpjs日期功能

You can use date.js 您可以使用date.js

There a method called toString , it accepts some format specifiers . 有一个名为toString的方法,它接受一些format specifiers

Code: 码:

var dateString = 'Dec 17, 2013'; // date string
var actualDate = new Date(dateString); // convert to actual date
var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()+7);
var newDate2 = newDate.toString("MMM d, yyyy");

alert(newDate2);

Demo: http://jsfiddle.net/MWQAE/ 演示: http//jsfiddle.net/MWQAE/

Actually you don't need any extra js libs. 实际上你不需要任何额外的js库。 Just native Javascript is enough. 只需原生Javascript即可。 Try 尝试

//Extend prototype so you can call it in any Date instance
Date.prototype.customFormat = function() {
    var months = ['Jan','Feb','Mar','Apr','Maj','Jun','Jul','Aug','Sep','Okt','Nov','Dec'],t = this;
    return [months[t.getMonth() % 12], " ", t.getDate(), ", ", t.getFullYear()].join("")
}

//Now
alert((new Date).customFormat());
//7 days after
alert((new Date(+new Date + 7*24*3600*1000)).customFormat());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM