Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moment.js - how do I build moment from date and time string?

If I have given date string MM-DD-YYYY and time string HH:mm A or H:m a, how do I build a moment object?

I tried moment('MM-DD-YYYY HH:mm A') which did not work.

like image 980
Amitava Avatar asked May 29 '13 08:05

Amitava


People also ask

How do I create a moment object from a date?

Creating a moment object with a specific date and time is possible by calling the moment() function with arguments. Like the JavaScript Date, a moment object can be created from the number of milliseconds since 1970/1/1. Another possibility is using an array [year, month, day, hour, minute, second, milliseconds] .

How do you convert a string to a moment object?

We can parse a string representation of date and time by passing the date and time format to the moment function. const moment = require('moment'); let day = "03/04/2008"; let parsed = moment(day, "DD/MM/YYYY"); console.


2 Answers

Try using the format (second) parameter moment("12-25-1995", "MM-DD-YYYY");

For details see http://momentjs.com/docs/#/parsing/string-format/

like image 120
Michi Avatar answered Sep 25 '22 12:09

Michi


To create a moment object of your desired time, do the following:

moment('05-17-2018 11:40 PM', 'MM-DD-YYYY hh:mm A') 

In this case '05-17-2018 11:40 PM' is the desired time.

like image 21
Suman Kundu Avatar answered Sep 25 '22 12:09

Suman Kundu