最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get luxon to output ISO date format without a colon in the timezone - Stack Overflow

programmeradmin0浏览0评论

How to generate dates using luxon in the following format

2020-12-03T16:35:40.426+0100

I've tried to use

let format = "yyyy-MM-dd\'T\'HH:mm:ss.SSSZ"
let str = date.toFormat(format)

but I get

2020-12-03T15:32:00.000+1

How to generate dates using luxon in the following format

2020-12-03T16:35:40.426+0100

I've tried to use

let format = "yyyy-MM-dd\'T\'HH:mm:ss.SSSZ"
let str = date.toFormat(format)

but I get

2020-12-03T15:32:00.000+1
Share Improve this question asked Dec 3, 2020 at 16:21 bradgonesurfingbradgonesurfing 32.3k20 gold badges122 silver badges232 bronze badges 3
  • Try the toIso() method of the DateTime object as mentioned in moment.github.io/luxon/docs/class/src/… – Thakkie Commented Dec 3, 2020 at 16:34
  • It outputs the timezoe as "01:00" instead of "0100" – bradgonesurfing Commented Dec 3, 2020 at 16:35
  • 1 I've found the correct format string ""yyyy-MM-dd\'T\'HH:mm:ss.SSSZZZ" – bradgonesurfing Commented Dec 3, 2020 at 16:35
Add a ment  | 

1 Answer 1

Reset to default 4

From Luxon's table of tokens

Z       narrow offset   +5
ZZ      short offset    +05:00
ZZZ     techie offset   +0500

Thus, you can use ZZZ to emit the offset in the format you requested.

However, be aware that bining this in the way that you asked would produce a string that is not pliant with ISO 8601.

ISO 8601 (in section 4.3.2) provides two valid formats, "Basic" and "Extended":

Basic format                Example
YYYYMMDDThhmmss             19850412T101530
YYYYMMDDThhmmssZ            19850412T101530Z
YYYYMMDDThhmmss±hhmm        19850412T101530+0400
YYYYMMDDThhmmss±hh          19850412T101530+04

Extended format             Example
YYYY-MM-DDThh:mm:ss         1985-04-12T10:15:30
YYYY-MM-DDThh:mm:ssZ        1985-04-12T10:15:30Z
YYYY-MM-DDThh:mm:ss±hh:mm   1985-04-12T10:15:30+04:00
YYYY-MM-DDThh:mm:ss±hh      1985-04-12T10:15:30+04

Most people use the extended format, and sometimes you will find the basic format (especially in URLs). But you have bined the date and time from the extended format with the offset from the basic format, which is not a supported bination.

Unless you are locked into this for some reason, I suggest moving to either the basic or extended format.

发布评论

评论列表(0)

  1. 暂无评论