[英]Convert UTC Date and Time to local Date and Time using Moment.js
[英]Processing localised date to convert it to local date/time using moment.js
我正在從后端獲取轉換的UTC日期,因此需要在將其顯示在屏幕上之前將日期轉換為本地化的本地時間格式。
我一直在用,
moment.locale(currentLocale); //Here currentLocale is the user's locale.
localtime = moment.utc(inputDate).toDate(); // inputDate = 2017年11月01日 AM10:42 (GMT)
moment(localtime).format(formatType); //formatType = "MMMM Do YYYY, h:mm A"
當日期為英文時,此功能很好,但翻譯后的日期卻無效。
有什么方法可以處理轉換后的日期,以將其轉換為語言環境的日期和時間,如果可能,還可以轉換數字。
您應該為解析器提供要解析的字符串的格式。 您必須忽略正在得到的錯誤。
var inputDate = 'inputDate = 2017年11月01日 AM10:42 (GMT)' var parseFormat = 'YYYY MM DD Ah:mm'; var printFormat = 'MMMM Do YYYY, h:mm A'; var d = moment.utc(inputDate, parseFormat).toDate(); console.log('Local: ' + moment(d).format(printFormat)); console.log('UTC : ' + moment.utc(d).format(printFormat));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.min.js"></script>
將字符串解析為moment對象,然后創建一個ECMAScript Date只是將其解析回moment.utc對象的技術對我來說很奇怪。 在沒有將輸出偏移量也設置為UTC的情況下,似乎沒有一種將字符串解析為UTC的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.