簡體   English   中英

在 NodeJS (JavaScript) 中轉換 yyyy-MM-dd'T'HH:mm:ss.SSS'Z' 的問題

[英]Issue with converting yyyy-MM-dd'T'HH:mm:ss.SSS'Z' in NodeJS (JavaScript)

我正在嘗試運行以下代碼:

// Included the data time formatter
var DateTimeFormatter = require('datetimeformatter');

var inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
var outputFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
var date = LocalDate.parse(gDate, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
var formattedDate = outputFormatter.format(date);
System.out.println(formattedDate);

當我運行代碼時,我得到以下TypeError: DateTimeFormatter.ofPattern is not a function

從網上搜索不准確,ofPattern應該是function??

我究竟做錯了什么?

試試這個命令:

npm install date-and-time

示例用法:

const date = require('date-and-time')
const bd = new Date();
const value = date.format(bd,'YYYY/MM/DD HH:mm:ss');
console.log("current date and time : " + value)

包含的代碼似乎表明您正在嘗試運行 Java 代碼。 您可以提取日期字符串,例如:

const newTime = new Date('2022-09-09T00:20Z').toLocaleDateString('en-gb');

可以多修改output結果,例如:`

const newTime = new Date('2019-02-19T06:00:00Z').toLocaleDateString('en-gb' {
year: 'numeric',
month: 'long',
day: 'numeric'
}); // 18 February 2019`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM