🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Date 对象常用操作 ## 时间格式 <span style="font-size: 20px;">GMT(格林尼治平时)</span> Greenwich Mean Time,GMT 是指位于英国伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。 由于地球每天的自转是有些不规则的,而且正在缓慢减速,因此格林尼治时间已经不再被作为标准时间使用。现在的标准时间,是由原子钟报时的协调世界时(UTC)。 所以我们也从 MDN 上的文档看到对于`toGMTString()`的解释是: > Returns a string representing the Date based on the GMT (UT) time zone. Use toUTCString() instead. <span style="font-size: 20px;">UTC(世界标准时间)</span> 协调世界时,又称世界标准时间或世界协调时间,简称UTC(从英文「Coordinated Universal Time」/法文「Temps Universel Cordonné」而来),是最主要的世界时间标准,其以原子时秒长为基础,在时刻上尽量接近于格林尼治平时 <span style="font-size: 20px;">CST(北京时间)</span> 北京时间,China Standard Time,中国标准时间。在时区划分上,属东八区,比协调世界时早 8 小时,记为 UTC+8。 不过这个 CST 这个缩写比较纠结的是它可以同时代表四个不同的时间: * Central Standard Time (USA) UT-6:00 * Central Standard Time (Australia) UT+9:30 * China Standard Time UT+8:00 * Cuba Standard Time UT-4:00 因此使用 CST 可能上传时间给后端可能会存在一些 [问题](https://www.cnblogs.com/sanshi/archive/2009/08/28/1555717.html) 所以前端传时间给后端尽量使用 UTC。 ## 时间戳 时间戳是指格林威治时间 1970 年 01 月 01 日 00 时 00 分 00 秒(北京时间 1970 年 01 月 01 日 08 时 00 分 00 秒)起至现在的秒数。(定义上是秒,JS 方法获取的是毫秒) ```js var d = new Date() console.log(d.getTime() + " milliseconds since 1970/01/01") // 1564904334582 milliseconds since 1970/01/01 // Date 对象的 valueOf() 方法返回毫秒级时间戳 console.log(d.valueOf()) // 1564904334582 ``` ## 将 Date 对象转换为字符串 ```js console.log(new Date()) // Sun Aug 04 2019 15:55:40 GMT+0800 (中国标准时间) console.log(new Date().toTimeString()) // 15:55:40 GMT+0800 (中国标准时间) console.log(new Date().toDateString()) // Sun Aug 04 2019 console.log(new Date().toUTCString()) // Sun, 04 Aug 2019 07:55:40 GMT console.log(new Date().toLocaleString()) // 2019/8/4 下午3:55:40 console.log(new Date().toLocaleTimeString()) // 下午3:55:40 console.log(new Date().toLocaleDateString()) // 2019/8/4 /** * toTimeString() 方法可把 Date 对象的时间部分转换为字符串,并返回结果。 * toDateString() 方法可把 Date 对象的日期部分转换为字符串,并返回结果。 * toUTCString() 方法可根据世界时 (UTC) 把 Date 对象转换为字符串,并返回结果。 * toLocaleString() 方法可根据本地时间把 Date 对象转换为字符串,并返回结果。 * toLocaleTimeString() 方法可根据本地时间把 Date 对象的时间部分转换为字符串,并返回结果。 * toLocaleDateString() 方法可根据本地时间把 Date 对象的日期部分转换为字符串,并返回结果。 */ ``` # Math 方法 | 方法 | 描述 | | :----| :---- | |floor(x)| 对数进行下舍入| |ceil(x) |对数进行上舍入| |round(x) |把数四舍五入为最接近的整数| |random() |返回 [0~1) 之间的随机数| |max(x,y)| 返回 x 和 y 中的最高值,可以传入多个参数| |min(x,y)| 返回 x 和 y 中的最低值| |abs(x)| 返回数的绝对值| |pow(x,y) |返回 x 的 y 次幂| |sqrt(x) |返回数的平方根| |sin(x) |返回数的正弦| |cos(x) |返回数的余弦| |tan(x)| 返回角的正切| |acos(x)| 返回数的反余弦值| |asin(x) |返回数的反正弦值| |atan(x) |以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值| |atan2(y,x) |返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)| |exp(x) |返回 e 的指数| |log(x)| 返回数的自然对数(底为e)| # 参考链接 [https://www.w3school.com.cn/jsref/jsref\_obj\_date.asp](https://www.w3school.com.cn/jsref/jsref_obj_date.asp) [https://www.jb51.net/article/84563.htm](https://www.jb51.net/article/84563.htm) [https://github.com/lishengzxc/bblog/issues/5](https://github.com/lishengzxc/bblog/issues/5) [https://www.cnblogs.com/sanshi/archive/2009/08/28/1555717.html](https://www.cnblogs.com/sanshi/archive/2009/08/28/1555717.html)