将日期时间转换为特定格式 - Javascript



>我有以下代码

new Date(1501796503599)

返回值

Fri Aug 04 2017 03:11:43 GMT+0530 (IST)

我需要将上述值显示为"Today, 3:11 am"。有没有办法做到这一点

var time = moment(1501796503599).calendar();
console.log(time);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>

Moment.js应该适合您的需求。 看看他们的文档。

这应该适合您:

moment().calendar(); 

你可以简单地使用javascript进行检查,没有任何依赖关系。

var yourDate = new Date(1501796503599); 
var today = new Date(); 
var isToday = (today.toDateString() == yourDate.toDateString()); 
if(isToday){
yourDate = "Today, " + yourDate.toLocaleTimeString();
}
else{
yourDate;
}

var monthName = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
$(function(){
var newDate = '';
newDate = new Date(1501796503599)
if (newDate != '')
var str = newDate.getDate() + "-" + monthName[newDate.getMonth()] + "-" + newDate.getFullYear()+
" " + newDate.getHours() + ":" + newDate.getMinutes() + ":" + newDate.getSeconds();
console.log(str);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

试试这个。

最新更新