我有这个日期2015/08/06 06:00
,我想将其转换为6th August 2015 at 6am
。我知道如何在PHP中执行此操作,但遇到jquery问题。
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var d = new Date('2015/08/06 06:00');
var d_formatted = d.getDate() + '/' + monthNames[d.getMonth()] '/' + d.getFullYear();
我如何获得日期后缀和时间?
答案 0 :(得分:2)
试试这段代码,我已经摆弄了你的需求http://jsfiddle.net/j6tb81qy/
function DateFormat(arg)
{
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
var Dates;
if(arg.getDate()==1)
Dates =arg.getDate() + 'st';
if(arg.getDate()==2)
Dates =arg.getDate() + 'nd';
if(arg.getDate()== 3)
Dates =arg.getDate() + ' rd';
if(arg.getDate()> 3)
Dates = arg.getDate() +'th'
var hours ='';
console.log(Dates)
hours = arg.getHours();
if(arg.getHours() >12)
{
hours = (arg.getHours() %12)+ ' PM'
}
else
{
hours =arg.getHours()+ ' AM'
}
Dates = Dates + ' ' + monthNames[arg.getMonth()] +' ' + arg.getFullYear().toString() + ' at ' + hours.toString();
return Dates;
}
var d = new Date();
var val= DateFormat(d);
alert(val);
//6th August 2015 at 6am
答案 1 :(得分:1)
我更喜欢使用moment.js来执行此操作
$(function () {
$('.date').each(function (index, dateElem) {
var $dateElem = $(dateElem);
var formatted = moment($dateElem.text(), 'MM-DD-YYYY').format('MMMM D');
$dateElem.text(formatted);
})
});
答案 2 :(得分:0)
使用纯javascript:
var d = new Date();
var n = d.toString();
n的结果将是:
Thu Aug 06 2015 12:52:33 GMT-0300 (BRT)
答案 3 :(得分:0)
使用Date
对象,有几种方法可以检索时间: -
getHours()
和getUTCHours()
getMinutes()
和getUTCMinutes()
getSeconds()
和getUTCSeconds()
getMilliseconds()
和getUTCMilliseconds()
参考文献: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date#Methods