js中对Date日期进行格式化和把日期转换为毫秒数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wlangmood/article/details/72879607

1、将中国标准时间格式化为(2017-06-06 15:05:04)

function formatDateTime(theDate) {

var _hour = theDate.getHours();

var _minute = theDate.getMinutes();

var _second = theDate.getSeconds();

var _year = theDate.getFullYear()

var _month = theDate.getMonth();

var _date = theDate.getDate();

if(_hour<10){_hour="0"+_hour ;}

if(_minute<10){_minute="0"+_minute;  }

if(_second<10){_second="0"+_second  }

扫描二维码关注公众号,回复: 3313908 查看本文章

_month = _month + 1;

if(_month < 10){_month = "0" + _month;}

if(_date<10){_date="0"+_date  }

return  _year + "-" + _month + "-" + _date + " " + _hour + ":" + _minute + ":" + _second ;

}

var time = new Date();

var formatTime = formatDateTime(time );

返回结果:

Tue Jun 06 2017 15:31:09 GMT+0800 (中国标准时间)

2017-06-06 15:31:09

2、把格式化时间转换为毫秒数

var formatTimeS = new Date('2017-06-06 15:31:09').getTime();

返回结果:1496734269900

3、把毫秒数转换为标准时间

var formatTimeS = new Date(1496734269900);

返回结果:

Tue Jun 06 2017 15:31:09 GMT+0800 (中国标准时间)

猜你喜欢

转载自blog.csdn.net/wlangmood/article/details/72879607