js时间转化

1 const defaultTicks = 621355968000000000;
2 
3 export function convertDateToTicks(date = new Date()) {
4     return date.getTime() * 10000 + defaultTicks;
5 }
6 
7 export function convertTicksToLocalDate(ticks) {
8     return new Date((ticks - defaultTicks) / 10000 + new Date().getTimezoneOffset() * 60 * 1000);
9 }
 1 export function formatDate(data, format) {
 2     var o = {
 3         "M+": data.getMonth() + 1, //month 
 4         "d+": data.getDate(), //day 
 5         "h+": data.getHours(), //hour 
 6         "m+": data.getMinutes(), //minute 
 7         "s+": data.getSeconds(), //second 
 8         "q+": Math.floor((data.getMonth() + 3) / 3), //quarter 
 9         "k": data.getDay(),
10         "S": data.getMilliseconds() //millisecond 
11     }
12 
13     if (/(y+)/.test(format)) {
14         format = format.replace(RegExp.$1, (data.getFullYear() + "").substr(4 - RegExp.$1.length));
15     }
16 
17     for (var k in o) {
18         if (new RegExp("(" + k + ")").test(format)) {
19             format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
20         }
21     }
22     return format;
23 }

 

posted @ 2018-07-31 14:58  漠然0408丶  阅读(132)  评论(0编辑  收藏  举报