Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format existing date variable with moment.js

I have a date variable (coming from an external source):

var date = '28/04/2017';
var time = '19:28';

It is possible to format these variables with moment.js (or without?) to variout formats?

Example: 04.28 19:28, 2017.04.28 19:28 or even Today at 19:28 (with moment().calendar();)

I tried

moment(date+' '+time).format('MM.DD.YYYY');

...but I am getting "Invalid date" error.

like image 915
Adrian Avatar asked Apr 27 '17 20:04

Adrian


People also ask

How can I get date in dd mm yyyy format in moment?

SearchDate = moment(new Date(), “DD/MM/YYYY”);

How can I get previous date in moment?

Just like this: moment(). subtract(1, 'days') . It will give you the previous day with the same exact current time that is on your local pc.


1 Answers

You're using moment(String) method, but you're not passing a Supported format that it expects to parse.

You should use moment(String, String), where the first String is the input date string, and second is the format of your input date String.

Try this:

moment(date+' '+time,'DD/MM/YYYY HH:mm').format('MM.DD.YYYY');
like image 172
Churro Avatar answered Oct 20 '22 18:10

Churro