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.
We can remove the time portion from a date with the startOf method. We pass in a date with the hour and minutes set into the moment function. Then we call startOf with the 'day' argument to get return a moment object with the time set to midnight of the same date.
Use the toDateString() method to remove the time from a date, e.g. new Date(date. toDateString()) . The method returns only the date portion of a Date object, so passing the result to the Date() constructor would remove the time from the date.
Format the date and time with Moment.js. One of the major strengths that Moment.js has over native JavaScript Date objects is how easy it is to format the output date and time. Just chain the format () method to a Moment date object and pass it a format string as a parameter:
Moment.js is a Swiss Army knife for working with dates in JavaScript. It allows you to parse, validate, manipulate, and display dates and times using a clean and concise API. In this article I’ll show you how to get up and running with Moment.js, as well as demonstrate several of its common use cases.
Get a date and time from a timestamp with Moment.js Similar to new Date (ms), you can pass the number of milliseconds since the epoch to moment (): const dayAfterEpoch = moment (86400000); If you want to get a date using a Unix timestamp in seconds, you can use the unix () method:
For people interesting in truncating a Javascript Date object (as in the question title): var d = new Date (); var startOfDay = new Date (d.getFullYear (), d.getMonth (), d.getDate ()); Moreover, the duplicate is definitely not the same question as it is about Moment.js. gives: Thu Jul 11 2019 00:00:00 GMT-0400 (Eastern Daylight Time)
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With