How to set Date of an existing moment.js object
Just use your Date
object as parameter:
var d = new Date();
var m = moment(d);
And voila, your moment object is set to the same time as your Date object.
It may be a little late, but you can transform your new date to an object (newDate.toObject()
) and pass it to the set
method of your previous moment object:
var m = moment(); // Initial moment object
// Create the new date
var myDate = new Date();
var newDate = moment(myDate);
// Inject it into the initial moment object
m.set(newDate.toObject());
- eg:1
moment("01/01/2000", "DD/MM/YYYY")
- eg:2
moment("01 Jan 2000")
for the OP, given a string formatted in a particular way, you simply pass in the string and its format to moment, such as:
moment("01-01-2000 23:45", "DD-MM-YYYY HH:MM")
There are many options for the mask, these are all listed in the momentjs docs. You can even pass an array of possible masks and moment will try them all to see if it can make sense of your original string.