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

javascript - Why is that I'm getting "Invalid DateTime" in the following LuxonMoment code? - Stack Ove

programmeradmin3浏览0评论
const dt = DateTime.fromISO(new Date(date))
// dt => DateTime {ts: 1516876197386, zone: LocalZone, loc: Locale, invalid: "unparsable", weekData: null, …}
return dt.toFormat('yyyy/mm/dd')

The result is: Invalid DateTime. Why is this and how to fix it?

Luxon's docs: .js~DateTime.html#instance-method-toFormat

const dt = DateTime.fromISO(new Date(date))
// dt => DateTime {ts: 1516876197386, zone: LocalZone, loc: Locale, invalid: "unparsable", weekData: null, …}
return dt.toFormat('yyyy/mm/dd')

The result is: Invalid DateTime. Why is this and how to fix it?

Luxon's docs: https://moment.github.io/luxon/docs/class/src/datetime.js~DateTime.html#instance-method-toFormat

Share Improve this question asked Jan 25, 2018 at 10:33 alexalex 7,60115 gold badges53 silver badges79 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 17

fromISO:

Create a DateTime from an ISO 8601 string

accepts ISO string, while you are passing a JavaScript Date.

You can use Date's toISOString() or luxon fromJSDate

const DateTime = luxon.DateTime;
const dt = DateTime.fromISO(new Date().toISOString());
console.log(dt.toFormat('yyyy/MM/dd'));
const dt2 = DateTime.fromJSDate(new Date());
console.log(dt2.toFormat('yyyy/MM/dd'));
<script src="https://moment.github.io/luxon/global/luxon.min.js"></script>

Moreover, note that you have to use uppercase MM to print month instead of lowercase mm that stands for minutes.

You can use fromJSDate like: luxon.DateTime.fromJSDate(new Date())

发布评论

评论列表(0)

  1. 暂无评论