JavaScript format date like in PHP
This question already has an answer here:
var arr=time.split(' ');
var date_arr=arr[0];
var time_arr=arr[1];
var temp_date=date_arr.split('-');
var temp_time=time_arr.split(':');
var js_date=temp_date[2]+'.'+temp_date[1]+'.'+temp_date[0]+' '+temp_time[0]+":"+temp_time[1];
You need to do all by your hands. Javascript's Date object has enough methods. So please try smth like this:
var dateTime = new Date(Date.parse("2014-02-14 18:37:48"));
var date = dateTime.getDate().toString().length > 1 ? dateTime.getDate() : '0' + dateTime.getDate();
var month = dateTime.getMonth().toString().length > 1 ? dateTime.getMonth() + 1 : '0' + (dateTime.getMonth() + 1);
var hours = dateTime.getHours().toString().length > 1 ? dateTime.getHours() : '0' + dateTime.getHours();
var minutes = dateTime.getMinutes().toString().length > 1 ? dateTime.getMinutes() : '0' + dateTime.getMinutes();
var formattedDate = date + '.' + month + '.' + dateTime.getFullYear() + ' ' + hours + ':' + minutes;
console.log(formattedDate);
链接地址: http://www.djcxy.com/p/8162.html
上一篇: 从服务中检索日期格式