![](/img/trans.png)
[英]Moment format the time will become invalid date without debug mode on React Native
[英]React native : moment not working for specific time value without debug mode
我有下面的代碼,我將時間值格式化為日期格式。
const arrivalDateTime = moment(
`${todaysDate.format("YYYY-MM-DD")} ${arrivalTime}:00:00`,
).toDate();
當調試模式關閉時,如果我 select 來自控件的 8 或 9 值,則無法格式化該值。
當我處於調試模式並且 select 8 或 9 時,它能夠將值格式化為:
Fri May 22 2020 09:00:00 GMT-0600 (Mountain Daylight Time)
我看到許多線程討論相同的問題,但其中提供的解決方案並沒有幫助我正確格式化。
我正在嘗試打印到達日期時間值,它在日志中顯示如下;
Date { NaN }
我正在嘗試這個,但它不起作用,它的日子 toDate 不是 function:
moment().format(`YYYY-MM-DD ${arrivalTime}:00:00`).toDate();
花了我一段時間,但我終於弄明白了。
這沒有用:
const arrivalDateTime = moment(
`${todaysDate.format("YYYY-MM-DD")} ${arrivalTime}:00:00`,
).toDate();
這有效:
const arrivalDateTime = moment(
`${todaysDate.format('YYYY/MM/DD')} ${arrivalTime}:00`,
).toDate();
// 請注意,引擎似乎不喜歡使用 ' - ' 進行解析。 所以我把它改成'/'
仍然不確定其背后的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.