admin管理员组

文章数量:1394052

I'm trying to convert a date to a readable format.

For example:

var dayjs = require('dayjs')

const date = '1989-08-12T01:00:00.000Z'
console.log(dayjs(date).format('DD/MM/YYYY'))

The result shows: 11/08/1989

Expected behavior A clear and concise description of what you expected to happen.

I expect that the log should return 12/08/1989 instead of 11/08/1989

Day.js Version: v1.8.16

Not sure if I miss any parameters or not. Thanks,

I'm trying to convert a date to a readable format.

For example:

var dayjs = require('dayjs')

const date = '1989-08-12T01:00:00.000Z'
console.log(dayjs(date).format('DD/MM/YYYY'))

The result shows: 11/08/1989

Expected behavior A clear and concise description of what you expected to happen.

I expect that the log should return 12/08/1989 instead of 11/08/1989

Day.js Version: v1.8.16

Not sure if I miss any parameters or not. Thanks,

Share Improve this question asked Oct 21, 2019 at 17:16 Dale NguyenDale Nguyen 1,9804 gold badges24 silver badges37 bronze badges 6
  • Try date = '1989-08-12'. What will be the result? – Daniyal Lukmanov Commented Oct 21, 2019 at 17:23
  • Your problem is your timezone .format format with your timezone. use .format('DD/MM/YYYY h:m:s') and see log, you will understand – hong4rc Commented Oct 21, 2019 at 17:25
  • 1 I think the problem is utc time difference. – Daniyal Lukmanov Commented Oct 21, 2019 at 17:25
  • @DaniyalLukmanov it returns 12/08/1989. I guess, it is one way to solve it. About UTC time, can you elaborate that? I'm reading about too. – Dale Nguyen Commented Oct 21, 2019 at 17:27
  • @Hongarc it shows 11/08/1989 9:0:0. Time is different...? – Dale Nguyen Commented Oct 21, 2019 at 17:28
 |  Show 1 more ment

1 Answer 1

Reset to default 5

Your input is timezone 0, but the output is your timezone. You can change your code to

var dayjs = require('dayjs')

const date = '1989-08-12T01:00:00.00' // remove 0Z
console.log(dayjs(date).format('DD/MM/YYYY'))

Your problem is the same with https://github./iamkun/dayjs/issues/323

本文标签: javascriptdayjs returns wrong date with formatStack Overflow