我想在 javascript 中创建一个 Date 对象,它代表 0001 年,即 2014 年前。
我试过
d = new Date(); d.setYear(1);
console.log(d);
但它给出了 1901 年
与
d = new Date(1,1,1)
console.log(d);
没办法。
如何创建这个日期?
最佳答案
首先,这根本不是 Y2K 问题!(更新:在某些情况下 - 它与 Y2K 问题有关,但这不是这里的问题)
正确答案是您不能可靠地做到这一点。夏令时适用于第 1 年吗?有多少个闰年?有没有?等等,但@Daniel 的回答将使用它!
更新:更不用说@MattJohnson 关于 DST 的帖子了。 DST in year 1, actually JS (ES5 anyway) will lie and use the current DST rule for all years
所以请不要自欺欺人地认为您可以可靠地处理低于 1970 年的日期。(即使在那个时间范围内,您也会遇到很多问题和惊喜。)
但如果你真的、真的需要,你可以使用 new Date('0001-01-01')(ISO 8601 格式)或@Daniel 的方法:
var d = new Date(); d.setFullYear(1);
但是在你使用它之前阅读这个...
在 JS 中有 4 种创建日期的方法:
new Date()
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
1) new Date() 创建当前日期(在您本地的时区),所以我们现在对它不感兴趣。
2) new Date(number) 创建一个新的日期对象作为零时间加上数字。零时间是 1970 年 1 月 1 日 00:00:00 UTC。
所以在这种情况下,JS 中的时间从 1970 年开始计算。
(new Date(0)).toUTCString()
"Thu, 01 Jan 1970 00:00:00 GMT
如果你使用负数,你可以“回到”1970 年之前的时间
(new Date(-62167219200000)).toUTCString()
"Sat, 01 Jan 0 00:00:00 GMT"
-62167219200000 <- milliseconds
-62167219200000 / 1000
-62167219200 <- seconds
-62167219200 / 60
-1036120320 <- minutes
-1036120320 / 60
-17268672 <- hours
-17268672 / 24
-719528 <- days
-719528 / 365
-1971.309589041096 <- years ( this is roughly calculated value )
问题是它不可靠。有多少个闰年?有没有?夏令时?等等,我不喜欢它,因为这个神奇的数字 -62167219200000
3) new Date(dateString) 这是最“可靠”的方式。 DateString - 表示 RFC2822 或 ISO 8601 日期的字符串。
RFC2822/IETF 日期语法(RFC2822 第 3.3 节),例如“格林威治标准时间 1995 年 12 月 25 日星期一 13:30:00”
它的问题是,如果您使用负数,您将在所有方法的结果中得到一个不正确的带有 NaN 的日期
(new Date('01 January -1 00:00:00 UTC')).getFullYear()
NaN
如果您使用高于或等于 0 且低于 50 的年份,则将自动添加 2000。
(new Date('01 January 0 00:00:00 UTC')).getFullYear()
2000
(new Date('01 January 1 00:00:00 UTC')).getFullYear()
2001
(new Date('01 January 10 00:00:00 UTC')).getFullYear()
2010
(new Date('01 January 01 00:00:00 UTC')).getFullYear()
2001
(new Date('01 January 30 00:00:00 UTC')).getFullYear()
2030
(new Date('01 January 49 00:00:00 UTC')).getFullYear()
2049
如果您使用高于或等于 50 且低于 100 的年份,则将添加 1900。
(new Date('01 January 50 00:00:00 UTC')).getFullYear()
1950
(new Date('01 January 51 00:00:00 UTC')).getFullYear()
1951
(new Date('01 January 90 00:00:00 UTC')).getFullYear()
1990
(new Date('01 January 99 00:00:00 UTC')).getFullYear()
1999
等于或大于 100 的年份将得到正确的年份数
(new Date('01 January 100 00:00:00 UTC')).getFullYear()
100
(new Date('01 January 101 00:00:00 UTC')).getFullYear()
101
(new Date('01 January 999 00:00:00 UTC')).getFullYear()
999
(new Date('01 January 9999 00:00:00 UTC')).getFullYear()
9999
因此我们无法使用 RFC2822/IETF 日期语法创建年份 1
关于 ISO 8601:
http://www.w3.org/TR/NOTE-datetime
实际格式是
Year:
YYYY (eg 1997)
Year and month:
YYYY-MM (eg 1997-07)
Complete date:
YYYY-MM-DD (eg 1997-07-16)
Complete date plus hours and minutes:
YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
Complete date plus hours, minutes and seconds:
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
Complete date plus hours, minutes, seconds and a decimal fraction of a second
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
我们最感兴趣的是“完成日期”
(new Date('0001-01-01')).toUTCString()
"Mon, 01 Jan 1 00:00:00 GMT"
雅虎!!! (或者最好说是 Google!:)),我们可以使用 ISO 8601 创建年份为 1 的日期
但要小心,不要尝试使用负数或短年份数字,因为对这些的解析可能会因本地化或精神错乱而有所不同 :)
(new Date('-0001-01-01')).toUTCString()
"Sun, 31 Dec 2000 21:00:00 GMT"
(new Date('01-01-01')).toUTCString()
"Sun, 31 Dec 2000 21:00:00 GMT"
(new Date('02-01-01')).toUTCString()
"Wed, 31 Jan 2001 21:00:00 GMT"
(new Date('02-01-05')).toUTCString()
"Mon, 31 Jan 2005 21:00:00 GMT"
4) new Date(年、月、日、时、分、秒、毫秒)
要使用这个,您必须传递两个参数(年和月),所有其他参数都是可选的。要小心,因为这里的月份将从 0 到 11 开始,与其他地方不同。哇? o_O
警告!此日期将以您当前的时区创建!!!所以要小心使用它!
UPD: @matt-johnson 的澄清
...actually the Date object always reflects the local time zone. You can't place it in another time zone, and even if you initialize it with a UTC timestamp, it will still reflect back the local time zone in most of the functions. Internally it tracks UTC by the numeric timestamp, and there are functions that expose UTC values explicitly, but everything else is local.
负数将被解释为负数
(new Date(-1, 0)).toString()
"Fri Jan 01 -1 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(-234, 0)).toString()
"Wed Jan 01 -234 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
0到99的数字会自动递增1900
(new Date(0, 0)).toString()
"Mon Jan 01 1900 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(1, 0)).toString()
"Tue Jan 01 1901 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(11, 0)).toString()
"Sun Jan 01 1911 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(50, 0)).toString()
"Sun Jan 01 1950 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(99, 0)).toString()
"Fri Jan 01 1999 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
从 100 到 275760 的数字将被解释为年份数字
(new Date(100, 0)).toString()
"Fri Jan 01 100 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(102, 0)).toString()
"Sun Jan 01 102 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(2002, 0)).toString()
"Tue Jan 01 2002 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
高于 275760 的数字将是无效日期
(new Date(275760, 0)).toString()
"Tue Jan 01 275760 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(275761, 0)).toString()
"Invalid Date"
更新:
new Date(Date.UTC(1,1,1)) 与 new Date(year, month, day, hours, minutes, seconds, milliseconds) 相同的症状会更安全。因为 Date.UTC 函数。
关于Javascript 日期 01/01/0001,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29968116/
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
为什么以下不同?Time.now.end_of_day==Time.now.end_of_day-0.days#falseTime.now.end_of_day.to_s==Time.now.end_of_day-0.days.to_s#true 最佳答案 因为纳秒数不同:ruby-1.9.2-p180:014>(Time.now.end_of_day-0.days).nsec=>999999000ruby-1.9.2-p180:015>Time.now.end_of_day.nsec=>999999998
是否有简单的方法来更改默认ISO格式(yyyy-mm-dd)的ActiveAdmin日期过滤器显示格式? 最佳答案 您可以像这样为日期选择器提供额外的选项,而不是覆盖js:=f.input:my_date,as::datepicker,datepicker_options:{dateFormat:"mm/dd/yy"} 关于ruby-on-rails-事件管理员日期过滤器日期格式自定义,我们在StackOverflow上找到一个类似的问题: https://s
我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时
我正在尝试使用在我的代码中是动态的Time.local来安排时间。在每个月的第一天,我传递的值是Time.local(2009,9,-1,0)。在PHP中,这会将时间设置为上个月的最后一天。在ruby中,我只是得到“ArgumentError:参数超出范围”。是我用错了方法还是什么?谢谢。 最佳答案 您应该使用DateTime类而不是Time。(您可能需要先require'date'并安装activesupportgem。)它比Time更通用,并且可以用DateTime.civil(2009,9-1,-1,0)做你想做的事。为天
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的