362
formatCalendarDate = function (dateTime) {
    return moment.utc(dateTime).format('LLL');
};

It displays: "28 februari 2013 09:24"

But I would like to remove the time at the end. How can I do that?

I'm using Moment.js.

Pang
  • 9,564
  • 146
  • 81
  • 122
Obsivus
  • 8,231
  • 13
  • 52
  • 97

18 Answers18

778

Sorry to jump in so late, but if you want to remove the time portion of a moment() rather than formatting it, then the code is:

.startOf('day')

Ref: http://momentjs.com/docs/#/manipulating/start-of/

Liam
  • 27,717
  • 28
  • 128
  • 190
Graham Charles
  • 9,394
  • 3
  • 26
  • 41
  • 110
    Be careful with this if you're going between timezones (or if you're not paying attention to timezones). I had an issue where my UTC date was getting converted to local time, *then* applying `startOf('day')`, which was then the start of the *previous* day. Fixed with `moment(moment.utc('2013-10-29T00:00:00+00:00').startOf('day').format('LL')).startOf('day').toDate()` – colllin Nov 06 '13 at 05:37
  • 43
    Also be careful that this function actually mutates the original object – Dirk Boer Jul 08 '15 at 16:36
  • 10
    `.startOf('day')` does not removes the time part per se, it just sets the time to 00:00:00. So, yes, as commented by 'collin', you have to be careful when saving date. Better alternative is using `format('LL')`, as have been answered in this thread. – Sudarshan_SMD Dec 13 '16 at 07:18
  • 7
    To avoid mutating the original object, use `someMoment.clone().startOf('day')` or `moment(someMoment).startOf('day')`. – Pang May 23 '17 at 08:06
  • Be careful, startOf('day') will reset everything and not just set the time to 00:00:00. `moment().utc().add(1,'d').startOf('day')` will reset the object to today. – Tristan Sep 20 '17 at 19:09
  • 2
    I also had to deal with multiple timezones but found that simply stringing utc().startOf('day') was enough: `moment("2017-10-09T00:00:00Z").isSameOrAfter(moment("2017-10-09 22:00:00+00:00").utc().startOf('day'), 'day');` true `moment("2017-10-09T00:00:00Z").isSameOrAfter(moment("2017-10-09 22:00:00+00:00").startOf('day'), 'day');` false – RubberDuckRabbit Oct 12 '17 at 17:27
  • This can be problematic with timezones, for example: `console.log(moment().tz('America/Los_Angeles').startOf('day').format());` spits out `2018-03-09T00:00:00-08:00` and `console.log(moment().startOf('day').format());` spits out `2018-03-10T00:00:00+08:00` – Jaime Cham Mar 10 '18 at 04:26
  • @Tristan That's not accurate, `startOf('day')` changes the object to the start of the day it was already pointing at. Possibly converting to utc made the day shift to yesterday and then adding 1d brought it back to today – Michael Mrozek Jun 26 '20 at 04:21
67

Use format('LL')

Depending on what you're trying to do with it, format('LL') could do the trick. It produces something like this:

Moment().format('LL'); // => April 29, 2016
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
  • 1
    See https://momentjs.com/docs/#/parsing/string-format/ for more formatting options. – LMB Jul 25 '20 at 14:30
47

The correct way would be to specify the input as per your requirement which will give you more flexibility.

The present definition includes the following

LTS : 'h:mm:ss A', LT : 'h:mm A', L : 'MM/DD/YYYY', LL : 'MMMM D, YYYY', LLL : 'MMMM D, YYYY h:mm A', LLLL : 'dddd, MMMM D, YYYY h:mm A'

You can use any of these or change the input passed into moment().format(). For example, for your case you can pass moment.utc(dateTime).format('MMMM D, YYYY').

Sahil Jain
  • 3,649
  • 2
  • 14
  • 16
31

Okay, so I know I'm way late to the party. Like 6 years late but this was something I needed to figure out and have it formatted YYYY-MM-DD.

moment().format(moment.HTML5_FMT.DATE); // 2019-11-08

You can also pass in a parameter like, 2019-11-08T17:44:56.144.

moment("2019-11-08T17:44:56.144").format(moment.HTML5_FMT.DATE); // 2019-11-08

https://momentjs.com/docs/#/parsing/special-formats/

Mix Master Mike
  • 1,079
  • 17
  • 26
11
formatCalendarDate = function (dateTime) {
    return moment.utc(dateTime).format('LL')
}
Pang
  • 9,564
  • 146
  • 81
  • 122
AmGates
  • 2,127
  • 16
  • 29
11

You can also use this format:

moment().format('ddd, ll'); // Wed, Jan 4, 2017

Hashmita Raut
  • 171
  • 1
  • 7
8

With newer versions of moment.js you can also do this:

var dateTime = moment();

var dateValue = moment({
    year: dateTime.year(),
    month: dateTime.month(),
    day: dateTime.date()
});

See: http://momentjs.com/docs/#/parsing/object/.

FlavorScape
  • 13,301
  • 12
  • 75
  • 117
8

Whenever I use the moment.js library I specify the desired format this way:

moment(<your Date goes here>).format("DD-MMM-YYYY")

or

moment(<your Date goes here>).format("DD/MMM/YYYY")

... etc I hope you get the idea

Inside the format function, you put the desired format. The example above will get rid of all unwanted elements from the date such as minutes and seconds

Adrian Grzywaczewski
  • 868
  • 1
  • 12
  • 25
6

You can use this constructor

moment({h:0, m:0, s:0, ms:0})

http://momentjs.com/docs/#/parsing/object/

console.log( moment().format('YYYY-MM-DD HH:mm:ss') )

console.log( moment({h:0, m:0, s:0, ms:0}).format('YYYY-MM-DD HH:mm:ss') )
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
acucchieri
  • 562
  • 4
  • 8
5

For people like me want the long date format (LLLL) but without the time of day, there's a GitHub issue for that: https://github.com/moment/moment/issues/2505. For now, there's a workaround:

var localeData = moment.localeData( moment.locale() ),
    llll = localeData.longDateFormat( 'llll' ),
    lll = localeData.longDateFormat( 'lll' ),
    ll = localeData.longDateFormat( 'll' ),
    longDateFormat = llll.replace( lll.replace( ll, '' ), '' );
var formattedDate = myMoment.format(longDateFormat);
AJ Richardson
  • 6,610
  • 1
  • 49
  • 59
  • 1
    Finally a sensible answer. Take my upvote sir! The issue hasn't been addressed yet it seems. – oyalhi Aug 01 '19 at 02:57
4

Try this:

moment.format().split("T")[0]
Mohd
  • 5,523
  • 7
  • 19
  • 30
  • 1
    Watch out with this method, as `1993-06-07T22:00:00.000Z` will result as `1993-06-07` whereas it is the start of the day of `1993-06-08` – Tom Nov 20 '17 at 07:49
2

The thing is - you can run into an issue with timezones. For example, if you parse date like this: '2022-02-26T00:36:21+01:00' it may turn into '25/02/2022' As a solution if your date is in ISO format you can just cut off the time portion from the string, like this:

moment('2022-02-26T00:36:21+01:00'.split('T')[0]).utc().format('DD/MM/YYYY')

This solution is quite blunt, so be careful with string format.

1

console.log( moment().format('YYYY-MM-DD HH:mm:ss') )

console.log( moment({h:0, m:0, s:0, ms:0}).format('YYYY-MM-DD HH:mm:ss') )
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
Luca
  • 11
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 08 '23 at 11:59
  • You should add explanation. – Super Kai - Kazuya Ito Jun 09 '23 at 00:01
0

This worked perfectly for me:

moment().format('YYYY-MM-DD')
cigien
  • 57,834
  • 11
  • 73
  • 112
Aviv
  • 129
  • 1
  • 11
0

Try

new Date().toDateString() 

Result - "Fri Jun 17 2022"

cigien
  • 57,834
  • 11
  • 73
  • 112
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Ethan Jun 17 '22 at 22:20
0

This format works pretty fine

const date = new Date();
const myFormat= 'YYYY-MM-DD';

const myDate = moment(date, 'YYYYMMDDTHHmmss').format(myFormat);
cigien
  • 57,834
  • 11
  • 73
  • 112
0

use Like this

moment().format("dd")

outPut : "Tu"

-3
moment(date).format(DateFormat)

Here DateFormat should be DateFormat = 'YYYY-MM-DD'

Abhinav Kumar
  • 2,883
  • 1
  • 17
  • 30
  • Hello, welcome to SO. Consider making a [Tour](https://stackoverflow.com/tour) understand how to answer questions. Please, give more information about your solution and use Stack Snippet to show your code. – Diesan Romero Jul 05 '21 at 14:34