js中日期转换为时间戳

 

 1 function dateToTimestamp(date) {
 2                //方法一
 3         var newDate = new Date();
 4         newDate.setFullYear(date.substring(0,4));
 5         newDate.setMonth(date.substring(5,7)-1);
 6         console.log('3--' + date.substring(8,10));
 7         newDate.setDate(date.substring(8,10)); 
 8         newDate.setHours(date.substring(11,13)); 
 9         newDate.setMinutes(date.substring(14,16)); 
10         newDate.setSeconds(date.substring(17,19));
11         console.log('date',newDate);
12         return Date.parse(newDate)/1000;
13                 //方法二
14         date = date.replace(/-/g,'/'); // 将-替换成/,因为下面这个构造函数只支持/分隔的日期字符串
15         date = new Date(date); // 构造一个日期型数据,值为传入的字符串
16         var time = date.getTime();
17         time = time / 1000;
18         return time;
19     }

 

你可能感兴趣的:(日期转换)