javascript:新的约会,缺少的一年 [英] javascript: new date, missing year

查看:110
本文介绍了javascript:新的约会,缺少的一年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打电话给新日期(1月4日)
,发现默认年份是2001年

I called new Date("Jan 4") and found the default year is 2001

a=new Date("Jan 4")
Thu Jan 04 2001 00:00:00 GMT-0500 (EST)

我有什么方法可以将默认年份设置为2011年?

Is there any way that I can set the default year to be 2011?

更新:
我知道我可以使用+ 2011
,但因为我有一些1月4日,而有些2008年1月4日
我只想使用2011年的前一种情况。

update: I know that I can use + 2011 but since I have some "Jan 4", and some "Jan 4, 2008" I only want to use 2011 for the former case.

但是,我知道如果你做Date(2008年1月4日2011),那么这一年将是2008年而不是2011年。这正是我所需要的。
无论如何,我会将第一个答案标记为正确。

However, I realize that if you do Date("Jan 4, 2008 2011") the year will be 2008 but not 2011. That's just what I need. I will mark the first answer correct anyway.

推荐答案

你能试试吗?

year = new Date().getFullYear(); // current year
a=new Date("Jan 4"+' '+year);

更新

您可以拆分字符串使用

'Jan 4, 2008'.split(',')[0]+' '+year;
'Jan 4'.split(',')[0]+' '+year; // still works

这篇关于javascript:新的约会,缺少的一年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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