心理

当前位置 /首页/完美生活/心理/列表

dayjs库如何设置默认时区

dayjs库如何设置默认时区

解决方式

安装依赖

npm install dayjs

1

1

注意:使用 UTC 需先配置 UTC 插件,才能正常运行

默认情况下, 会把时间解析成本地时间。

如果想使用 UTC 时间,您可以调用 () 而不是 dayjs()。

例子:

var utc = require('dayjs/plugin/utc')

nd(utc)

// 默认当地时间

dayjs()at() //2019-03-06T17:11:55+08:00

// UTC 模式

()at() // 2019-03-06T09:11:55Z

// 将本地时间转换成 UTC 时间

dayjs()()at() // 2019-03-06T09:11:55Z

1

2

3

4

5

6

7

8

9

10

11

1

2

3

4

5

6

7

8

9

10

11

代码编写

比如:目前有UTC格式的时间 2019-03-06T00:00:00Z

transformTime: (timestamp) => {

constdayjs = require('dayjs')

const utc = require('dayjs/plugin/utc')

nd(utc)

return (timestamp)at('YYYY/MM/DD HH:mm:ss')

}

transformTime(񟭓-03-06T00:00:00Z

TAG标签:设置 时区 默认 dayjs #