Javascript 日期加 2 周(14 天)
我用它来获取日期:
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
alert(month + "/" + day + "/" + year);
如何添加 2 周?因此,不要显示 10/13/2011,而是显示 10/27/2011 等
这是小提琴: http://jsfiddle .net/25wNa/
我希望一个输入具有 +14 天,另一个输入具有 +21 天
注意:我希望格式为 > 2011 年 10 月 13 日 <.
I use this to get the date:
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
alert(month + "/" + day + "/" + year);
How can I add 2 weeks ? So instead of showing 10/13/2011, to show 10/27/2011 etc
Here is the fiddle: http://jsfiddle.net/25wNa/
I want the one input to have +14 days and the other +21
Note: I'd like the format to be > 10/13/2011 <.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
12096e5
是一个 幻数,以毫秒为单位表示 14 天。jsFiddle。
12096e5
is a magic number which is 14 days in milliseconds.jsFiddle.
试试这个:
Try this:
已经为你制作了一把小提琴http://jsfiddle.net/pramodpv/wfwuZ/
have made a fidle for you http://jsfiddle.net/pramodpv/wfwuZ/
12096e5
是一种神奇的数字。仅 14 天以指数表示法表示的毫秒。这个数字是1000[ms] * 60[s] * 60[m] * 24[h] * 14[d]以指数形式保存的结果。
如果您输入<强>数字('12096e5')。您将得到 1209600000 [ms],这正好是 2 周(以毫秒为单位)。指数符号使其变得模糊。
您可以用指数表示法写出任何其他数字,以使其他开发人员的生活更加有趣。
Date 对象 具有接受毫秒作为参数的构造函数,该参数可以采用指数表示法。
两者是相同的。
12096e5
is a kind of magic number. Just 14 days in milliseconds in exponential notation.This number is the result of 1000[ms] * 60[s] * 60[m] * 24[h] * 14[d] saved in exponential notation.
You can check it if you type Number('12096e5'). You will get 1209600000 [ms] which is exactly 2 weeks in milliseconds. The exponential notation makes it obscure.
You can write any other number in exponential notation to make the life of your fellow developers more interesting.
Date object has constructor which accepts milliseconds as an argument which argument can be in exponential notation.
Both are the same.
嗯,JS 时间以毫秒为单位,因此添加两周就是计算出两周以毫秒为单位,然后添加该值的情况。
当然,如果您需要添加月份,则此方法会失败,因为它们的长度是可变的,但添加天和周就可以了。
或者,您可以使用 DateJS 库,它具有正是此类功能的功能(并且可以加载更多内容)。
使用 DateJS,您的代码可能如下所示:
希望有帮助。
Well, JS times are in millisecond, so adding two weeks would be a case of working out what two weeks is in milliseconds, and adding that value.
Of course, this method falls down if you need to add months, since they're variable in length, but it's fine for adding days and weeks.
Alternatively, you use the DateJS library, which has functionality for exactly this kind of thing (plus loads more).
With DateJS, your code could look like this:
Hope that helps.
从当前日期添加或减去 2 周
下面的代码示例以 YYYY-MM-DD 格式给出输出
如果在字符串中添加条件以将 0 与月份连接起来小于 10 的天。
Add or Subtract 2 weeks from current date
Below code example give output in YYYY-MM-DD format
If condition is added in the string to concatenate 0 with Month and Day which is less than 10.
添加以下原型方法
并且使用起来非常简单,
add the following prototype method
and than its very simple to use,
如果您要以特定格式格式化 javascript 日期,那么我想您可以看看这个脚本 http://blog.stevenlevithan.com/archives/date-time-format。包含脚本后您需要做的就是这个
new Date(+new Date + 1000* 60 * 60 * 24 * 14).format('dd/mm/yyyy')
,您将获取输出"27/10/2011"
该脚本非常小,缩小后仅略高于 1KB。这是工作小提琴的链接 http://jsfiddle.net/naryad/GufvT/
If you are formatting a javascript date in a particular format, then I think you can have a look at this script http://blog.stevenlevithan.com/archives/date-time-format. All you would need to do after including the script is this
new Date(+new Date + 1000* 60 * 60 * 24 * 14).format('dd/mm/yyyy')
and you would get the output"27/10/2011"
The script is pretty small, just above 1KB minified. This is the link to a working fiddle http://jsfiddle.net/naryad/GufvT/