将一天添加到 Javascript 日期 + 唯一格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6427113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding one day to Javascript date + unique formatting?
提问by Walker
I've inherited a project for a company I'm working for. Their dates are recorded in the following format:
我为我工作的公司继承了一个项目。它们的日期以下列格式记录:
March 18th, 2011 would be listed as "18 Mar 2011".
2011年3月18日将被列为“2011年3月18日”。
April 31st, 2010 would be listed as "31 Apr 2010".
2010 年 4 月 31 日将被列为“2010 年 4 月 31 日”。
How would I use Javascript to add one day to a date formatted in the above manner, then reconvert it back into the same format?
我将如何使用 Javascript 将一天添加到以上述方式格式化的日期,然后将其重新转换回相同的格式?
I want to create a function that adds one day to "18 Mar 2011" and returns "19 Mar 2011". Or adds 1 day to "30 Jun 2011" and returns "1 Jul 2011".
我想创建一个函数,将一天添加到“2011 年 3 月 18 日”并返回“2011 年 3 月 19 日”。或者将 1 天添加到“30 Jun 2011”并返回“1 Jul 2011”。
Can anyone help me out?
谁能帮我吗?
回答by Gabriele Petrioli
First of all there is no 31st of April ;)
首先,没有 4 月 31 日;)
To the actual issue, the date object can understand the current format when passed as an argument..
对于实际问题,日期对象在作为参数传递时可以理解当前格式。
var dateString = '30 Apr 2010'; // date string
var actualDate = new Date(dateString); // convert to actual date
var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()+1); // create new increased date
// now extract the bits we want to crete the text version of the new date..
var newDateString = ('0'+newDate.getDate()).substr(-2) + ' ' + newDate.toDateString().substr(4,3) + ' ' + newDate.getFullYear();
alert(newDateString);
demo at http://jsfiddle.net/gaby/jGwYY/1/
演示在http://jsfiddle.net/gaby/jGwYY/1/
The same extraction using (the better supported) slice
instead of substr
使用(更好的支持)slice
代替相同的提取substr
// now extract the bits we want to crete the text version of the new date..
var newDateString = ('0'+newDate.getDate()).slice(-2) + ' ' + newDate.toDateString().slice(4,7) + ' ' + newDate.getFullYear();
Demo at http://jsfiddle.net/jGwYY/259/
回答by Justin Ethier
回答by Pete Hodgson
Most (all?) browsers will be able to parse that date string in with a simple
大多数(全部?)浏览器将能够使用简单的方式解析该日期字符串
var parsedDate = new Date(dateString);
Once you have a Date object you can add a day and output a formatted date string using something like underscore.date.
一旦你有了一个 Date 对象,你就可以添加一天并使用underscore.date 之类的东西输出一个格式化的日期字符串。
If you discover that some browsers can't parse that date format then you can write a pretty simple regex that will pull apart the date string into its constituent parts, and then build a Date instance by hand.
如果您发现某些浏览器无法解析该日期格式,那么您可以编写一个非常简单的正则表达式,将日期字符串分解为其组成部分,然后手动构建一个 Date 实例。
Also I would strongly recommend doing the parsing in a separate function, and to try and keep dates in a Date representation as much as possible. Parse the string into a date as soon as you can, and format it back into a string as late as you can.
此外,我强烈建议在单独的函数中进行解析,并尽可能将日期保留在 Date 表示中。尽快将字符串解析为日期,并尽可能晚地将其格式化回字符串。