在没有Date对象的情况下在Javascript中操作日期
似乎我无法使用javascript Date对象,因为当您使用日期字符串初始化时,它固有地默认为美国日期.无法将任何区域性信息传递给日期对象
It appears I can't use the javascript Date object as it inherintly defaults to US dates when you initialise with a datestring. There is no way of passing any culture information to the date object
即无论客户端的语言环境设置是什么
I.e. No matter what the clients locale settings are
var d = new Date("08/10/2009")将始终创建代表2009年8月10日而不是2009年10月8日的日期对象(如果客户的语言环境是英国).
var d = new Date("08/10/2009") will always create a date object representing the 10th August 2009 rather than the 8th October 2009 if the clients locale was the UK.
因此,鉴于我的要求是能够轻松地添加/减去天/月/年,是否有一种聪明的方法可以轻松地执行此操作而无需使用Date对象
So given that my requirement is to be able to add/subtract days/months/years easily is there a clever way of doing this easily without the Date object
我需要做的就是在日期(或日期的字符串表示形式)中添加一天.因此,如果我的代码检测到语言环境设置在美国,则当它看到类似"10/08/2009"的字符串时,会将其重命名为"10/09/2009",但是如果检测到它在英国,它将知道它是日期的英国字符串表示形式,并将其修改为"09/10/2009"
All i need to do is add a day to a date (or a string representation of a date). so if my code detects the locale setttings are in the US, when it sees a string like "10/08/2009" it whacks it up to "10/09/2009" but if it had detected it was in the UK it would have know it a uk string representation of a date and whacked it up to "09/10/2009"
对于JavaScript上的日期操作和本地化,我总是推荐 DateJS 库.
For date manipulation and localization on JavaScript I always recommend the DateJS library.
该库抽象了Date对象的用法,具有很好的本地化选项,强大的日期解析和格式设置,并且还具有非常出色的流利API.
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.