72

How can i change the current date to this format(DD/MM/YYYY) using moment.js?

I have tried below code.

$scope.SearchDate = moment(new Date(), "DD/MM/YYYY");

But it's return 0037-11-24T18:30:00.000Z. Did't help to format current date.

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234

6 Answers6

166

You need to call format() function to get the formatted value

$scope.SearchDate = moment(new Date()).format("DD/MM/YYYY")
//or $scope.SearchDate = moment().format("DD/MM/YYYY")

The syntax you have used is used to parse a given string to date object by using the specified formate

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
49

You can use this

moment().format("DD/MM/YYYY");

However, this returns a date string in the specified format for today, not a moment date object. Doing the following will make it a moment date object in the format you want.

var someDateString = moment().format("DD/MM/YYYY");
var someDate = moment(someDateString, "DD/MM/YYYY");
Shateel Ahmed
  • 1,264
  • 2
  • 12
  • 23
  • 2
    I have date format in 'MM/DD/YY' and wants to convert it to 'DD/MM/YYYY'. So my doubt is how will moment know what format is it in. Suppose the date is '06/05/20'. How will moment know if it's MM/DD/YY or DD/MM/YY – Naman Jain Jul 21 '20 at 06:57
15

This worked for me

var dateToFormat = "2018-05-16 12:57:13"; //TIMESTAMP

moment(dateToFormat).format("DD/MM/YYYY"); // you get "16/05/2018"
atfede
  • 411
  • 6
  • 9
10

This actually worked for me:

moment(mydate).format('L');
t_plusplus
  • 4,079
  • 5
  • 45
  • 60
4

for anyone who's using react-moment:

simply use format prop to your needed format:

const now = new Date()
<Moment format="DD/MM/YYYY">{now}</Moment>
HiLuLiT
  • 433
  • 4
  • 7
0

A safe way to do this

moment.locale('en-US');
moment().format("L"); 

"06/23/2021"

moment.locale('fr');
moment().format("L");

"23/06/2021"

hassan khademi
  • 1,156
  • 12
  • 14