简体   繁体   中英

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

I am trying to run the following code:

// 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);

when I run the code i get the following TypeError: DateTimeFormatter.ofPattern is not a function

From searching online that is not accurate, ofPattern should be a function??

What am I doing wrong?

Try this command:

npm install date-and-time

Example usage:

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)

Included code seems to suggest you are trying to run Java code. You can extract the date string for example:

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

You can modify the output result more, for example: `

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM