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.
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] .
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.
Try using the format (second) parameter moment("12-25-1995", "MM-DD-YYYY");
For details see http://momentjs.com/docs/#/parsing/string-format/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With