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

Date Casting in Javascript - Stack Overflow

programmeradmin1浏览0评论
alert(new Date("8/14/2012 4:24:34 PM"));
Tue Aug 14 2012 16:24:34 GMT+0800 (Malay Peninsula Standard Time)


alert(new Date("14/08/2012 4:31:29 PM"));
Invalid Date

I tried date.parseExact from datejs, it doesn't work.

Is there any other way to cast "14/08/2012 4:31:29 PM" to a Date object?

alert(new Date("8/14/2012 4:24:34 PM"));
Tue Aug 14 2012 16:24:34 GMT+0800 (Malay Peninsula Standard Time)


alert(new Date("14/08/2012 4:31:29 PM"));
Invalid Date

I tried date.parseExact from datejs, it doesn't work.

Is there any other way to cast "14/08/2012 4:31:29 PM" to a Date object?

Share Improve this question asked Aug 14, 2012 at 8:38 Jerry LamJerry Lam 4521 gold badge7 silver badges22 bronze badges 2
  • "I tried date.parseExact from datejs" - what arguments did you use? Show use the code for this as well – Eric Commented Aug 14, 2012 at 8:42
  • date.parseExact("14/08/2012 4:31:29 PM", "dd/MM/yyyy h:mm:ss tt"); – Jerry Lam Commented Aug 14, 2012 at 8:48
Add a ment  | 

2 Answers 2

Reset to default 4

How about a quick swap using a regular expression?

alert(new Date("14/08/2012 4:31:29 PM".replace(/^(\d+)\/(\d+)/,'$2/$1')));

Rather than Date.parseExact have you tried using Date.parse function provided by the datejs library like so

Date.parse("14/08/2012 4:31:29 PM")
发布评论

评论列表(0)

  1. 暂无评论