Loading

JavaScript时间戳、日期相互转换

开发过程中会遇到很多需要时间戳和标准时间相互转化的需求,稍大型项目会引入类似moment.js,但对于简单h5页面,更多的需要简单的方法来处理日期

1.日期格式化,时间戳转日期,类似 "1512053866000"转化为"2017-11-30 22:57:46"

所需参数
timeStr "September 16,2016"、"2016/09/16 14:15:05"、"2016/09/16"、'2014-04-23T18:55:49'和毫秒
dateSeparator 年、月、日之间的分隔符,默认为"-"
timeSeparator 时、分、秒之间的分隔符,默认为":"

  

 

 

 

 

 

 

 1 function getFormatDate(timeStr, dateSeparator, timeSeparator) {
 2     dateSeparator = dateSeparator ? dateSeparator : "-";
 3     timeSeparator = timeSeparator ? timeSeparator : ":";
 4     var date = new Date(timeStr),
 5             year = date.getFullYear(),// 获取完整的年份(4位,1970)
 6             month = date.getMonth(),// 获取月份(0-11,0代表1月,用的时候记得加上1)
 7             day = date.getDate(),// 获取日(1-31)
 8             hour = date.getHours(),// 获取小时数(0-23)
 9             minute = date.getMinutes(),// 获取分钟数(0-59)
10             seconds = date.getSeconds(),// 获取秒数(0-59)
11             Y = year + dateSeparator,
12             M = ((month + 1) > 9 ? (month + 1) : ('0' + (month + 1))) + dateSeparator,
13             D = (day > 9 ? day : ('0' + day)) + ' ',
14             h = (hour > 9 ? hour : ('0' + hour)) + timeSeparator,
15             m = (minute > 9 ? minute : ('0' + minute)) + timeSeparator,
16             s = (seconds > 9 ? seconds : ('0' + seconds)),
17             formatDate = Y + M + D + h + m + s;
18     return formatDate;
19 }

//测试代码
let timer = Date.parse(new Date()); //1512053866000
 console.log(getFormatDate(timer));  //"2017-11-30 22:57:46"
 1 function getCommonTime(milliseconds) {
 2     var date = new Date(milliseconds),
 3             time = date.toLocaleString(),//这种方法获取的时间格式根据电脑的不同而有所不同
 4             formatTime = getFormatDate(date);//获取格式化后的日期
 5     return time;
 6 }
 7 
 8 //测试代码
 9
10 getCommonTime(1512053866000);  //"2017/11/30 下午10:57:46"

2. 日期格式转化时间戳,类似"September 16,2016"、"2016/09/16 14:15:05"、"2016/09/16"、"2016-04-23 18:55:49"

 1 function getMilliSeconds(timeStr) {
 2     var date = new Date(timeStr),
 3             ms = date.getTime();//or ms=date.valueOf();返回指定时间距离1970年1月1日0时0分0秒的毫秒
 4     return ms;
 5 }
 6 //测试代码
 7 var     timeStr=1398250549490,
 8         timeStr1 = "September 16,2016 14:15:05",
 9         timeStr2 = "September 16,2016",
10         timeStr3 = "2016/09/16 14:15:05",
11         timeStr4 = "2016/09/16",
12         timeStr5 = '2014-04-23T18:55:49'13 console.log("普通时间转换成时间戳:" + getMilliSeconds(timeStr1) + "、" + getMilliSeconds(timeStr2) + "、" + getMilliSeconds(timeStr3) + "、" + getMilliSeconds(timeStr4) + "、" + getMilliSeconds(timeStr5));
14 
15 //格式化日期转时间戳
16 /*
17  * timeStr:时间,字符串,例如:"2016-09-16 14:15:05"
18  * */
19 function getFormatMilliSeconds(timeStr) {
20     var dateStr = timeStr.replace(/-/g, '/'),
21             milliSeconds;
22     //method1:
23     milliSeconds = Date.parse(dateStr);
24     //method2:
25     //var date = new Date(dateStr);
26     //milliSeconds = date.getTime();//or milliSeconds=date.valueOf()
27     return milliSeconds;
28 }
29 //测试代码
30 console.log('格式化后的时间转时间戳:'+getFormatMilliSeconds('2016-04-23 18:55:49'));//1461408949000

 

 

posted @ 2017-11-30 17:30  lewiskycc  阅读(482)  评论(0编辑  收藏  举报