本文介绍了从JavaScript获取毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的日期格式2011-07-29T08:18:39



我想转换这个日期介绍毫秒
我尝试



  var myDate ='2011-07-29T08:18:39'; 
new Date(myDate.replace(T,))getTime()

但显示我NaN。
请帮助
谢谢

解决方案

T.J。是正确的我的原始解决方案在IE9和Safari中失败。这样做会在所有主流浏览器中运行。

  var myDate ='2011-07-29T08:18:39' ; 

函数parseDate(dateString){
var time = Date.parse(dateString);
如果(!time){
time = Date.parse(dateString.replace(T,));
if(!time){
bound = dateString.indexOf('T');
var dateData = dateString.slice(0,bound).split(' - ');
var timeData = dateString.slice(bound + 1,-1).split(':');

time = Date.UTC(dateData [0],dateData [1] -1,dateData [2],timeData [0],timeData [1],timeData [2]);
}
}
返回时间;
}

var milliseconds = parseDate(myDate);


I have a date format like this 2011-07-29T08:18:39

I want to convert this date intro millisecondsI try

var myDate = '2011-07-29T08:18:39';    
new Date(myDate.replace("T"," ")).getTime()

but display me NaN.Please helpThanks

解决方案

T.J. was correct; my original solution failed in IE9 and Safari. This will do it, and works in all major browsers.

var myDate = '2011-07-29T08:18:39';    

function parseDate(dateString){
    var time = Date.parse(dateString);
    if(!time){
        time = Date.parse(dateString.replace("T"," "));
        if(!time){
            bound = dateString.indexOf('T');
            var dateData = dateString.slice(0, bound).split('-');
            var timeData = dateString.slice(bound+1, -1).split(':');

            time = Date.UTC(dateData[0],dateData[1]-1,dateData[2],timeData[0],timeData[1],timeData[2]);
        }
    }
    return time;
}

var milliseconds = parseDate(myDate);

这篇关于从JavaScript获取毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 10:41