为什么会给出三个不同的结果?
var hyphen=new Date('2014-04-03').toUTCString();
var slash=new Date('2014/04/03').toUTCString();
var backslash=new Date('2014\04\03').toUTCString();
console.log('hyphen= '+hyphen+ '\n' +'slash= '+slash+ '\n' + 'backslash= '+backslash);
最佳答案
Note: parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies. Support for RFC 2822 format strings is by convention only. Support for ISO 8601 formats differs in that date-only strings (e.g. "1970-01-01") are treated as UTC, not local.
除此之外,\0
是 null
字符,因此 '2014\04\03'
等于 '2014_4_3'
,其中 _
是空字符。将包含空字符的字符串解析为日期的结果根本没有定义。
关于javascript - 日期字段中的斜杠、反斜杠、连字符有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42973993/