DD/MM/YYYY Date format in Moment.js
Asked Answered
P

7

73

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.

Prolonge answered 25/4, 2015 at 6:13 Comment(0)
S
168

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

Saury answered 25/4, 2015 at 6:15 Comment(4)
ok. the new Date() is javascript code. can you give me for get current date by using moment.js?Prolonge
@RameshRajendran it gives you the current date - jsfiddle.net/arunpjohny/hrrnenf2/1Saury
@RameshRajendran $scope.SearchDate = moment(moment.now()).format("DD/MM/YYYY")Shorten
moment() is the same as moment(new Date())Hooker
S
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");
Scales answered 18/7, 2017 at 7:9 Comment(1)
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/YYTobytobye
C
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"
Corabelle answered 9/8, 2018 at 5:13 Comment(0)
I
10

This actually worked for me:

moment(mydate).format('L');
Injurious answered 27/6, 2017 at 9:59 Comment(1)
This will return MM/DD/YYYY. But in the actual question he is expecting DD/MM/YYYY format.Listed
T
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>
Tumescent answered 18/1, 2021 at 16:34 Comment(0)
J
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"

Jaco answered 20/11, 2022 at 11:55 Comment(0)
G
0

The best way is:

let today = new Date();
let todayFormatted = moment(today).format('DD-MM-YYYY');
let startDateFormatted = moment(startDate).format('DD-MM-YYYY');
const isStartDateToday = (todayFormatted === startDateFormatted);
Georginegeorglana answered 13/12, 2023 at 15:33 Comment(2)
Except for this startDate thing which can be found nowhere in the question, this answer does not have anything new compared to existing answers.Dragline
I made a confusion. My solution is the best way of compare two dates is the samePrognosticate

© 2022 - 2025 — McMap. All rights reserved.