JavaScript/Date:修订间差异
来自康健生活
小 文字替换 -“{{leftrightflex|”替换为“{{Double-columns|” |
|||
| 第5行: | 第5行: | ||
=== Date === | === Date === | ||
如谷歌浏览器,可这样处理日期转时间戳。 | 如谷歌浏览器,可这样处理日期转时间戳。 | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript">new Date('2021-12-20 12:12:12'); | ||
new Date('2021-12-20 12:12:12').getTime();</syntaxhighlight>|{{consoleout|<pre>Mon Dec 20 2021 12:12:12 GMT+0800 (中国标准时间) | new Date('2021-12-20 12:12:12').getTime();</syntaxhighlight>|{{consoleout|<pre>Mon Dec 20 2021 12:12:12 GMT+0800 (中国标准时间) | ||
1639973532000 | 1639973532000 | ||
</pre>}}}} | </pre>}}}} | ||
但是 Safari 浏览器这样处理会抛出异常。 | 但是 Safari 浏览器这样处理会抛出异常。 | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript">new Date('2021-12-20 12:12:12'); | ||
new Date('2021-12-20 12:12:12').getTime(); | new Date('2021-12-20 12:12:12').getTime(); | ||
</syntaxhighlight>|{{consoleout|<pre>Invalid Date | </syntaxhighlight>|{{consoleout|<pre>Invalid Date | ||
| 第16行: | 第16行: | ||
</pre>}}}} | </pre>}}}} | ||
Safari 中不支持 <code>-</code> 作为日期格式的连接字符串,但是支持 <code>/</code> 。eq。 | Safari 中不支持 <code>-</code> 作为日期格式的连接字符串,但是支持 <code>/</code> 。eq。 | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript">new Date('2021/12/20 12:12:12'); | ||
new Date('2021/12/20 12:12:12').getTime(); | new Date('2021/12/20 12:12:12').getTime(); | ||
</syntaxhighlight>|{{consoleout|<pre>Mon Dec 20 2021 12:12:12 GMT+0800 (CST) | </syntaxhighlight>|{{consoleout|<pre>Mon Dec 20 2021 12:12:12 GMT+0800 (CST) | ||
| 第23行: | 第23行: | ||
==== data 转 iso ==== | ==== data 转 iso ==== | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
new Date().toISOString().replace(/\..+/,'') + 'Z'</syntaxhighlight>|{{consoleout|<pre>'2022-07-05T03:49:08Z' | new Date().toISOString().replace(/\..+/,'') + 'Z'</syntaxhighlight>|{{consoleout|<pre>'2022-07-05T03:49:08Z' | ||
</pre>}}}} | </pre>}}}} | ||
==== 13 转 10 ==== | ==== 13 转 10 ==== | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
parseInt((new Date()).getTime() / 1000);</syntaxhighlight>|{{consoleout|<pre>1656993199 | parseInt((new Date()).getTime() / 1000);</syntaxhighlight>|{{consoleout|<pre>1656993199 | ||
</pre>}}}} | </pre>}}}} | ||
==== 转普通日期 ==== | ==== 转普通日期 ==== | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
// 1656993199 毫秒时间戳 | // 1656993199 毫秒时间戳 | ||
new Date(parseInt(1656993199) * 1000).toLocaleString().replace(/:\d{1,2}$/,'');</syntaxhighlight>|{{consoleout|<pre>'2022/7/5 11:53' | new Date(parseInt(1656993199) * 1000).toLocaleString().replace(/:\d{1,2}$/,'');</syntaxhighlight>|{{consoleout|<pre>'2022/7/5 11:53' | ||
| 第39行: | 第39行: | ||
=== 时间戳 === | === 时间戳 === | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
Date.parse(new Date()); | Date.parse(new Date()); | ||
(new Date()).valueOf(); | (new Date()).valueOf(); | ||
| 第51行: | 第51行: | ||
=== ISODate === | === ISODate === | ||
==== ISO8601 ==== | ==== ISO8601 ==== | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
new Date().toISOString();</syntaxhighlight>|{{consoleout|<pre> | new Date().toISOString();</syntaxhighlight>|{{consoleout|<pre> | ||
'2022-05-26T09:12:18.954Z' | '2022-05-26T09:12:18.954Z' | ||
| 第57行: | 第57行: | ||
去掉毫秒。 | 去掉毫秒。 | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
new Date().toISOString().replace(/\..+/,'') + 'Z';</syntaxhighlight>|{{consoleout|<pre> | new Date().toISOString().replace(/\..+/,'') + 'Z';</syntaxhighlight>|{{consoleout|<pre> | ||
'2022-05-26T09:13:01Z' | '2022-05-26T09:13:01Z' | ||
2026年11月26日 (四) 09:18的最新版本
引言
方法
getMonth()
getMonth 是以数组的形式存储的值,所以下标会少 1。
Date
如谷歌浏览器,可这样处理日期转时间戳。
new Date('2021-12-20 12:12:12');
new Date('2021-12-20 12:12:12').getTime();
Mon Dec 20 2021 12:12:12 GMT+0800 (中国标准时间) 1639973532000
但是 Safari 浏览器这样处理会抛出异常。
new Date('2021-12-20 12:12:12');
new Date('2021-12-20 12:12:12').getTime();
Invalid Date NaN
Safari 中不支持 - 作为日期格式的连接字符串,但是支持 / 。eq。
new Date('2021/12/20 12:12:12');
new Date('2021/12/20 12:12:12').getTime();
Mon Dec 20 2021 12:12:12 GMT+0800 (CST) 1639973532000
data 转 iso
new Date().toISOString().replace(/\..+/,'') + 'Z'
'2022-07-05T03:49:08Z'
13 转 10
parseInt((new Date()).getTime() / 1000);
1656993199
转普通日期
// 1656993199 毫秒时间戳
new Date(parseInt(1656993199) * 1000).toLocaleString().replace(/:\d{1,2}$/,'');
'2022/7/5 11:53'
时间戳
Date.parse(new Date());
(new Date()).valueOf();
new Date().getTime();
function
Date.parse
扩展
ISODate
ISO8601
new Date().toISOString();
'2022-05-26T09:12:18.954Z'
去掉毫秒。
new Date().toISOString().replace(/\..+/,'') + 'Z';
'2022-05-26T09:13:01Z'
参考
时间戳
| 一天后 | 示例 | new Date().getTime() + 86400 * 1000;
|
| 七天后 | 示例 | new Date().getTime() + 86400 * 7 * 1000;
|
| 一百年后 | 示例 | new Date().getTime() + 86400 * 31 * 12 * 100 * 1000;
|
No categories