简体   繁体   中英

How to convert time and date string to ISO format

I have two inputs, a time and a date input. I'm trying to format them as an ISO string to send to the backend using moment.js .

This is what I have so far 01:00 2016-01-01 , I need to format or convert that to ISO. Is there a way to do it using Moment?

To convert ISO I recommend the more standard

date.format();

or

JSON.stringify(yourDate)

or if you prefer momentjs:

var date = moment();
date.toISOString();

or

moment(yourDate).format('MM/DD/YYYY'); // <- your custom format string

To know what are the momentjs formatting rules start reading here

假设您所指的是ISO8601和momentjs2.10.6 ),则目前我是这样做的

var example = momentObject.format("YYYY-MM-DD[T]HH:mm:ss");

You need to use moment's parse function to first create the correct moment object from the data that you have (assuming a 24-hour clock, and month listed before the days):

var myMoment = moment("01:00 2016-01-01", "HH:mm YYYY-MM-DD");

Then you can use moment's format function to output the date in the ISO format that you want. Note that calling the format function without any parameters will output ISO 8601 by default:

myMoment.format();

See the moment docs for more info here .

Hope this helps!

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