Format the 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';

      

Is it possible to format these variables with moment .js (or without?) To display 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 get an Invalid Date error.

+3


source to share


4 answers


You are using the method moment(String)

, but you are not passing the Supported format that it expects to parse.

You should use moment(String, String)

where the first line is the date input string and the second is the input string format.



Try the following:

moment(date+' '+time,'DD/MM/YYYY HH:mm').format('MM.DD.YYYY');

      

+8


source


Try:



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

      

+1


source


You need to keep the same format :

var date = '04-28-2017'; // Month/day/year
var time = '19:28';
console.log(   moment(date, 'MM-DD-YYYY').format('MMMM D') ) // April 28

      

0


source


how to store the moment date in a variable like var date = moment (). formate ();

0


source







All Articles