问题描述
我有一个var example =05-10-1983
如何获得字符串示例?
我尝试使用Date对象...但没有...
I've try to use Date object...but nothing...
推荐答案
这样做可以像简单的场景一样:
This would do it for simple scenarios like the one you have:
var example = '05-10-1983';
var date = new Date();
var parts = example.split('-');
date.setFullYear(parts[2], parts[0]-1, parts[1]); // year, month (0-based), day
date.setTime(date.getTime() + 86400000);
alert(date);
本质上,我们创建一个空的,并使用功能。然后我们使用并添加1天(86400000毫秒),并使用功能。
Essentially, we create an empty Date object and set the year, month, and date with the setFullYear()
function. We then grab the timestamp from that date using getTime()
and add 1 day (86400000 milliseconds) to it and set it back to the date using the setTime()
function.
如果您需要比这更复杂的东西,如支持对于不同的格式和类似的东西,您应该看看,它做了很多工作对于你。
If you need something more complicated than this, like support for different formats and stuff like that, you should take a look at the datejs library which does quite a bit of work for you.
这篇关于javascript - 获取第二天的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!