Simplest way to parse a Date in Javascript

后端 未结 4 1695
小蘑菇
小蘑菇 2020-12-06 19:10

I want to parse a Date chosen by user:

var ds = \"11 / 08 / 2009\";

I use

var d = new Date(ds);

It gives

相关标签:
4条回答
  • 2020-12-06 19:57

    Extend date to return values in your desired format.

    Here is a great article on how to do so. It includes code snippets.

    0 讨论(0)
  • 2020-12-06 20:08

    There are lots of libraries and copy-and-paste javascript snippets on the net for this kind of thing, but here is one more.

    function dateParse(s) {
      var parts = s.split('/');
      var d = new Date( parts[2], parts[1]-1, parts[0]);
      return d;
    }
    
    0 讨论(0)
  • 2020-12-06 20:09

    I have had success with DateJS. In particular, you would want to use parseExact, which allows you to provide a format string describing the meaning of each segment (so that you can map one segment to day and another to month).

    0 讨论(0)
  • 2020-12-06 20:15

    This article shows ways to convert date format from one to the other.

    0 讨论(0)
提交回复
热议问题