convert iso date to milliseconds in javascript

后端 未结 10 1923
不知归路
不知归路 2020-11-29 00:40

Can I convert iso date to milliseconds? for example I want to convert this iso

2012-02-10T13:19:11+0000

to milliseconds.

Because I

相关标签:
10条回答
  • 2020-11-29 01:26
    var date = new Date()
    console.log(" Date in MS last three digit = "+  date.getMilliseconds())
    console.log(" MS = "+ Date.now())
    

    Using this we can get date in milliseconds

    0 讨论(0)
  • 2020-11-29 01:28

    In case if anyone wants to grab only the Time from a ISO Date, following will be helpful. I was searching for that and I couldn't find a question for it. So in case some one sees will be helpful.

    let isoDate = '2020-09-28T15:27:15+05:30';
    let result = isoDate.match(/\d\d:\d\d/);
    console.log(result[0]);
    

    The output will be the only the time from isoDate which is,

    15:27
    
    0 讨论(0)
  • 2020-11-29 01:30

    A shorthand of the previous solutions is

    var myDate = +new Date("2012-02-10T13:19:11+0000");
    

    It does an on the fly type conversion and directly outputs date in millisecond format.

    Another way is also using parse method of Date util which only outputs EPOCH time in milliseconds.

    var myDate = Date.parse("2012-02-10T13:19:11+0000");
    
    0 讨论(0)
  • 2020-11-29 01:30

    Another option as of 2017 is to use Date.parse(). MDN's documentation points out, however, that it is unreliable prior to ES5.

    var date = new Date(); // today's date and time in ISO format
    var myDate = Date.parse(date);
    

    See the fiddle for more details.

    0 讨论(0)
  • 2020-11-29 01:33

    if wants to convert UTC date to milliseconds
    syntax : Date.UTC(year, month, ?day, ?hours, ?min, ?sec, ?milisec);
    e.g :
    date_in_mili = Date.UTC(2020, 07, 03, 03, 40, 40, 40);
    console.log('miliseconds', date_in_mili);

    0 讨论(0)
  • 2020-11-29 01:34

    Another possible solution is to compare current date with January 1, 1970, you can get January 1, 1970 by new Date(0);

    var date = new Date(); 
    var myDate= date - new Date(0);
    
    0 讨论(0)
提交回复
热议问题