Manipulating dates in Javascript without the Date object

馋奶兔 提交于 2019-12-02 04:14:37

For date manipulation and localization on JavaScript I always recommend the DateJS library.

This library abstracts the use of Date objects, has a very good localization options, powerful date parsing and formatting, and it also has a very nice fluent API.

If you know you are getting input formatted dd/mm/yyyy you can easily assemble the correct date.

function britDay(D){
 D= D.match(/\d+/g);
 return new Date(+D[2], D[1]-1, +D[0]);
}

toLocaleDateString will return the date in the format expected by the user.

Relying on the user input that obeys particular formatting rules is optimistic- which is why most sites use separate, labeled inputs or select fields for the month, date and year.

You probably know that it's easy to add one day to a date, just add 86,400 * 1000 milliseconds to the date. It sounds like displaying in your locale is the issue; does Date.toLocaleString() not do the right thing for you?

dojo.date.locale.parse will be able to parse a formatted string according the locale of your choice. It has a table of cultural data based off unicode.org/cldr. See this article for more information.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!