在没有Date对象的情况下在Javascript中操作日期 [英] Manipulating dates in Javascript without the Date object

查看:74
本文介绍了在没有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.

这篇关于在没有Date对象的情况下在Javascript中操作日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆