最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Simplest way to parse a Date in Javascript - Stack Overflow

programmeradmin1浏览0评论

I want to parse a Date chosen by user:

var ds = "11 / 08 / 2009";

I use

var d = new Date(ds);

It gives me November, 08, 2009. But what I need is August, 11, 2009.

What is the simplest way to parse the date?

I want to parse a Date chosen by user:

var ds = "11 / 08 / 2009";

I use

var d = new Date(ds);

It gives me November, 08, 2009. But what I need is August, 11, 2009.

What is the simplest way to parse the date?

Share Improve this question asked Aug 11, 2009 at 3:11 BillyBilly 15.7k28 gold badges72 silver badges101 bronze badges 1
  • Are dates always going to be in DD/MM/YYYY format? – Mark Biek Commented Aug 11, 2009 at 3:25
Add a comment  | 

3 Answers 3

Reset to default 20

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;
}

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).

Extend date to return values in your desired format.

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

发布评论

评论列表(0)

  1. 暂无评论