javascript - 日期字段中的斜杠、反斜杠、连字符有什么区别?

标签 javascript html date datetime

为什么会给出三个不同的结果?

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);

最佳答案

MDN: Date

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.

除此之外,\0null 字符,因此 '2014\04\03' 等于 '2014_4_3',其中 _ 是空字符。将包含空字符的字符串解析为日期的结果根本没有定义。

关于javascript - 日期字段中的斜杠、反斜杠、连字符有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42973993/

相关文章:

MySQL:比较日期字符串?

javascript - 当我不单击图像时如何停止声音播放?

javascript - 如何根据通过函数传递的参数更改 Jquery 选择器

html - 由于流媒体网站导致 Jsoup 延迟

javascript - 注销 Google 帐户时使用的功能称为什么?

date - 在本地时区获取 Haskell 中的日期

php - PHP 的 strtotime() 中的第一个工作日是什么?

Javascript 代码不适用于 phonegap

javascript - 删除id包含特定符号的<tr>

javascript - 使用 JavaScript 编辑 iFrame 元素(同域)