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.
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.
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')
.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. –
Stakeout someMoment.clone().startOf('day')
or moment(someMoment).startOf('day')
. –
Nudism moment().utc().add(1,'d').startOf('day')
will reset the object to today. –
Sclerotomy 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 –
Bleareyed 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
–
Antihelix 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 –
Gabion 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
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')
.
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
formatCalendarDate = function (dateTime) {
return moment.utc(dateTime).format('LL')
}
You can also use this format:
moment().format('ddd, ll'); // Wed, Jan 4, 2017
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()
});
date: dateTime.date()
instead of day: dateTime.date()
? –
Whiffen day
works pre version 2.8.4. –
Haul 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
L
, LL
, etc.) –
Barry DD/MM/YYYY
(two M
s) as this doesn't break when comparing dates with different locales, the other variants fail. –
Paige 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>
moment({h:0, m:0, s:0, ms:0})
would have given me January 1 1970 rather than today. –
Clam moment({ h: 0 })
does the same. –
Clam Omitted units default to 0 or the current date, month, and year.
–
Clam 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);
Try this:
moment.format().split("T")[0]
1993-06-07T22:00:00.000Z
will result as 1993-06-07
whereas it is the start of the day of 1993-06-08
–
Midwinter 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.
This worked perfectly for me:
moment().format('YYYY-MM-DD')
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>
Try
new Date().toDateString()
Result - "Fri Jun 17 2022"
This format works pretty fine
const date = new Date();
const myFormat= 'YYYY-MM-DD';
const myDate = moment(date, 'YYYYMMDDTHHmmss').format(myFormat);
moment(date).format(DateFormat)
Here DateFormat should be DateFormat = 'YYYY-MM-DD'
© 2022 - 2024 — McMap. All rights reserved.
startOf('day')
, which was then the start of the previous day. Fixed withmoment(moment.utc('2013-10-29T00:00:00+00:00').startOf('day').format('LL')).startOf('day').toDate()
– Batrachian