Moment JS getting Date without time [duplicate] - datetime

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')
Ref: http://momentjs.com/docs/#/manipulating/start-of/

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

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
https://momentjs.com/docs/#/parsing/special-formats/

You can also use this format:
moment().format('ddd, ll'); // Wed, Jan 4, 2017

formatCalendarDate = function (dateTime) {
return moment.utc(dateTime).format('LL')
}

Look at these Examples.
Format Dates
moment().format('MMMM Do YYYY, h:mm:ss a'); // December 7th 2020, 9:58:18 am
moment().format('dddd'); // Monday
moment().format("MMM Do YY"); // Dec 7th 20
moment().format('YYYY [escaped] YYYY'); // 2020 escaped 2020
moment().format(); // 2020-12-07T09:58:18+05:30
Relative Time
moment("20111031", "YYYYMMDD").fromNow(); // 9 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 8 years ago
moment().startOf('day').fromNow(); // 10 hours ago
moment().endOf('day').fromNow(); // in 14 hours
moment().startOf('hour').fromNow(); // an hour ago
Calendar Time
moment().subtract(10, 'days').calendar(); // 11/27/2020
moment().subtract(6, 'days').calendar(); // Last Tuesday at 9:58 AM
moment().subtract(3, 'days').calendar(); // Last Friday at 9:58 AM
moment().subtract(1, 'days').calendar(); // Yesterday at 9:58 AM
moment().calendar(); // Today at 9:58 AM
moment().add(1, 'days').calendar(); // Tomorrow at 9:58 AM
moment().add(3, 'days').calendar(); // Thursday at 9:58 AM
moment().add(10, 'days').calendar(); // 12/17/2020
Multiple Locale Support
moment.locale(); // en
moment().format('LT'); // 9:58 AM
moment().format('LTS'); // 9:58:18 AM
moment().format('L'); // 12/07/2020
moment().format('l'); // 12/7/2020
moment().format('LL'); // December 7, 2020
moment().format('ll'); // Dec 7, 2020
moment().format('LLL'); // December 7, 2020 9:58 AM
moment().format('lll'); // Dec 7, 2020 9:58 AM
moment().format('LLLL'); // Monday, December 7, 2020 9:58 AM
moment().format('llll'); // Mon, Dec 7, 2020 9:58 AM

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

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/.

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>

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]

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 format works pretty fine
const date = new Date();
const myFormat= 'YYYY-MM-DD';
const myDate = moment(date, 'YYYYMMDDTHHmmss').format(myFormat);

Try
new Date().toDateString()
Result - "Fri Jun 17 2022"

This worked perfectly for me:
moment().format('YYYY-MM-DD')

moment(date).format(DateFormat)
Here DateFormat should be DateFormat = 'YYYY-MM-DD'

Related

MomentJS not returning correct info

I have an ISO formatted date that isn't being converted to the correct time, and I can't figure out why.
const m = (() => {
console.log("Here is the original time: " + periods)
const timeFixed = moment(periods).format("dddd, MMMM Do YYYY, h:mm:ss ");
console.log("Here is the fixed time: " + timeFixed)
return timeFixed;
})();
Using this, returns this:
Here is the original time: 2020-08-12T08:52:55Z
Here is the fixed time: Wednesday, January 1st 2020, 12:00:00
Any help given is appreciated as always.
Solved it, weirdly. It seems Moment was having an issue with the incoming format. Converted it to a string, then it handled it.
const m = (() => {
console.log("Here is the original time: " + periods)
const timeString = new Date(periods);
const timeFixed = moment(timeString).format("dddd, MMMM Do YYYY, hh:mm:ss ");
console.log("Here is the fixed time: " + timeFixed)
return timeFixed;
})();
Resulted in:
Here is the original time: 2020-08-12T08:52:55Z
Here is the fixed time: Wednesday, August 12th 2020, 09:52:55
Yes the time zone is off, but that's another problem for another day. Thanks for helping me find it.

Momentjs - Add days with specific time

I'm using momentjs to calculate certain dates. For example:
moment().add(7, 'days');
This returns date in 7 days in same time like now:
Thu May 21 2020 12:06:35 GMT+0200.
What is the function for adding days but in specific time like:
Thu May 21 2020 17:00:00 GMT+0200.
You could chain the add() to get add hours,minutes,seconds.
console.log('Current time: ', moment().format("DD-MM-YYYY hh:mm:ss"));
var start_time = moment()
.add(1, "days")
.add(4, "hours")
.add(21, "seconds");
console.log('modified time: ', start_time.format("DD-MM-YYYY hh:mm:ss"));
<script src="https://momentjs.com/downloads/moment.js"></script>
I found a solution:
moment({ hour: 17 }).add(7, 'd')
This ads exactly 7 days to today in 17:00:00.

How to get friday to friday weeks from two date range using moment

I am trying to get week start as friday & end date as friday and I tried to use startOf/endOf week(week/isoweek) but failed. Is there any way that I can get friday as start of week and Friday as end of week using moment.
Moment(date).startOf('week'); // or isoweek
Output should be,
Date of friday
Request data:
First date= 05-09-2019
End date= 05-15-2019(current date)
Expected output:
[
{
Weekstart: 05-03-2019,
Weekend: 05-10-2019
},
{
Weekstart: 05-10-2019,
Weekend: 05-17-2019
}
]
There is no option for setting start day of week . But you can fetch last Friday date using
moment().weekday(-2).format("YYYY-DD-MM")
You can update the week start for a locale using something like:
moment.updateLocale(moment.locale(), { week: { dow: 5 } })
moment().startOf('week').toString(); // Fri May 10 2019 00:00:00 GMT+0100
You can do a short custom function to always give you start and end of the week based on a passed isoWeekDay:
let getCustomWeek = (dayOfWeek=7, date=new Date()) => {
let firstDay, lastDay, passedDay = moment(date).isoWeekday(dayOfWeek)
firstDay = moment(passedDay).subtract(1, 'week')
lastDay = moment(firstDay).add(1, 'week')
return { start: firstDay.format(), end: lastDay.format() }
}
let dateRange = ['05-09-2019', '05-15-2019']
console.log('Friday: ', dateRange.map(d => getCustomWeek(5, new Date(d))))
console.log('Wednesday: ', dateRange.map(d => getCustomWeek(3, new Date(d))))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>

Moment.js wierd translation

I'm using moment.js and getting something strange:
Input string:
'Wed, 30 Aug 2017 19:53:54 EST'
Want parse it using Moment.js:
moment('Wed, 30 Aug 2017 19:53:54 EST', 'ddd, DD MMM YYYY HH:mm:ss z');
getting object:
_d: Wed May 31 2017 23:59:59 GMT+0300 (FLE Daylight Time) {}
_f: "ddd, DD MMM YYYY HH:mm:ss z"
_i: "Wed, 30 Aug 2017 19:53:54 EST"
_isAMomentObject: true
_isUTC: false
_isValid: true
_i - it's input
_f - as I can understand - format
_d - it's date, result of parsing, WHY there 'May 31'?
Found and fixed problem. If someone faced with same problem, they could check my solution.
Datetime object mutate in controller:
let myDate = moment();
------------ skip ------------
let someObj = {
yesterday: myDate.subtract(1, 'days')
};
Because it's object, definitions like below doesn't helps:
const myDate = moment();
or
let myDate = moment();
Object.freeze(myDate);
only work solution for me is:
let myDate = moment();
------------ skip ------------
let someObj = {
yesterday: myDate.clone().subtract(1, 'days')
};

Getting the month number by month name with Moment.js

I am trying to return the month number passing the month name using MomentJS. For example if I pass "July" to moment() I would expect 7 to be returned.
After reading through the docs I tried several different ways, and this way came close...
console.log(moment().month("July"));
In the console, buried in the response I could see this...
_monthsParse: Array[7]
Could anyone please tell me how to return the month number using MomentJS correctly?
Try :
moment().month("July").format("M");
Relevant documentation: http://momentjs.com/docs/#/get-set/month/
alert(moment().month("July").format("M"));
<script src="https://momentjs.com/downloads/moment.min.js"></script>
Anybody looking to get month name from month number then you can try :
const number = 1; // 0 = Jan & 11 = Dec
moment().month(number).format("MMM"); // Feb
Use following to get full month name :
const number = 1; // 0 = January & 11 = December
moment().month(number).format("MMMM"); // February
To use simple month number try this:
const month = 2 //Feb
moment(month, 'M').format('MMMM');
##get month name in moment js with node js
moment() give today date
format("DD-MMMM-YYYY") / output 18-May-2020
format("DD-MM-YYYY") / output 18-05-2020
- sperator you can use /
```
var moment = require('moment');
m_date = moment().format("DD-MMMM-YYYY");
console.log("moment date :", m_date)
```
##output
```
moment date : 18-May-2020
```
Read Officail Docdescription here

Resources