我注意到JavaScript的new Date()功能在接受多种格式的日期时非常聪明.
Xmas95 = new Date("25 Dec, 1995 23:15:00")
Xmas95 = new Date("2009 06 12,12:52:39")
Xmas95 = new Date("20 09 2006,12:52:39")
Run Code Online (Sandbox Code Playgroud)
在调用new Date()函数时,我无法在任何地方找到显示所有有效字符串格式的文档.
这用于将字符串转换为日期.如果我们看一下相反的方面,即将日期对象转换为字符串,直到现在我的印象是JavaScript没有内置的API来将日期对象格式化为字符串.
编者注:以下方法是提问者的企图,关于特定浏览器的工作,但也不会在一般的工作; 请参阅此页面上的答案以查看一些实际解决方案.
今天,我在toString()日期对象上使用了该方法,并且令人惊讶的是它用于将日期格式化为字符串.
var d1 = new Date();
d1.toString('yyyy-MM-dd'); //Returns "2009-06-29" in Internet Explorer, but not Firefox or Chrome
d1.toString('dddd, MMMM ,yyyy') //Returns "Monday, June 29,2009" in Internet Explorer, but not Firefox or Chrome
Run Code Online (Sandbox Code Playgroud)
在这里,我找不到任何关于我们可以将日期对象格式化为字符串的方法的文档.
列出Date()对象支持的格式说明符的文档在哪里?
我在 Windows 10 中工作,并尝试使用 npm 脚本进行 git commit,其中包含包含提交日期和时间的消息:
"deploy": "cd dist && git add . && git commit -m \"Release at $(date)\" && git push"
Run Code Online (Sandbox Code Playgroud)
结果 git commit 消息是
在 $(date) 发布
代替
于 03/06/2019 11:43:57 发布
我在终端中运行相同的命令时得到:
git commit -m "Release at $(date)"
Run Code Online (Sandbox Code Playgroud)
什么是跨平台解决方案?
你能解释导致不同结果的原因吗?
date ×1
date-format ×1
datetime ×1
git ×1
javascript ×1
npm-scripts ×1
terminal ×1
time-format ×1
windows ×1