Comparing two dates with javascript or datejs (date difference)

后端 未结 3 2245
孤街浪徒
孤街浪徒 2021-02-20 16:36

I am trying to compare two dates which are in Finnish time form like this: dd.mm.YYYY or d.m.YYYY or dd.m.YYYY or d.mm.YYYY.

I am having a hard time finding out how to d

3条回答
  •  情歌与酒
    2021-02-20 17:03

    If you are using Datejs, and the optional time.js module, you can run your calculations with the following code by creating a TimeSpan object:

    Example

    // dd.mm.YYYY or d.m.YYYY
    // dd.m.YYYY or d.mm.YYYY
    
    var start = Date.parse("20.09.2011"); 
    var end = Date.parse("28.09.2011");
    
    var span = new TimeSpan(end - start);
    
    span.days; // 8
    

    Of course the above could be simplified down to one line if you really want to be extra terse.

    Example

    new TimeSpan(Date.parse(end) - Date.parse(start)).days; // pass 'end' and 'start' as strings
    

    Hope this helps.

提交回复
热议问题