admin管理员组文章数量:1431413
I have a string 10/11/2012
meaning November 10, 2012.
But when I do new Date("10/11/2012")
it returns October 11th.
How do I pass in the date format I want? In this case dd-mm-yyyy
I have a string 10/11/2012
meaning November 10, 2012.
But when I do new Date("10/11/2012")
it returns October 11th.
How do I pass in the date format I want? In this case dd-mm-yyyy
- You can't. You could extend the native javascript date parser so it bees possible though. – user1726343 Commented Nov 9, 2012 at 20:00
- A little bit of a tangent, but the topic is how to handle locale when dealing with Date objects stackoverflow./questions/6356839/… Remember, date objects represent a date in time, but that time is represented as a different hour of day depending on where you are – mrk Commented Nov 9, 2012 at 20:09
3 Answers
Reset to default 2I found jQuery.datepicker.parseDate(format, Date) at this site:
http://docs.jquery./UI/Datepicker/$.datepicker.parseDate
So I will be using the jQuery datepicker instead.
Unfortunately, there's no JavaScript Date constructor that allows you to pass in culture information so that it uses localized date formats. Your best bet is to use the constructor that takes the year, month, and day separately:
var parts = dateString.split('/');
var date = new Date(parseInt(parts[2], 10),
parseInt(parts[1], 10),
parseInt(parts[0], 10));
For this specific case, you can use:
var dateparts = date.split("/");
var datestring = dateparts[1] + "/" + dateparts[0] + "/" + dateparts[2];
var date = new Date(datestring);
In the more general case, you can extend the Date prototype, as demonstrated in this answer:
https://stackoverflow./a/13163314/1726343
本文标签: javascript string to date using custom formatStack Overflow
版权声明:本文标题:javascript string to date using custom format - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745490295a2660573.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论