I'm not getting the start of and end of day in MomentJS - momentjs

When I run this;
console.log(moment().startOf('day'))
console.log(moment().endOf('day'))
I get this:
2022-02-17T16:00:00.000Z
2022-02-18T15:59:59.999Z
Today is 18th Feb 2022 in my local time. How do I get 2022-02-18T00:00:00.000Z as startof and 2022-02-18T23:59:59.999Z as endof? Thanks

Related

Moment - Get the start of day of the local time and add a different timezone

Imagine that we have a timestamp, for example 1571140569 (Tuesday, 15 October 2019 11:56:09). I need to get the start, in this case Tuesday, 15 October 2019 00:00:00. Then i need to apply timezone, imagine "America/Lima" (GTM -7h), we want to show Tuesday, 14 October 2019 07:00:00
I use this code
moment_timezone(1571140569).startOf("day").tz("America/Lima");
But doesn´t work. Return Tuesday, 15 October 2019 00:00:00.
So: Somebody know how can i get the start of timestamp and then apply timezone?
Hope this helps you to get the output you are expecting.
const date = moment(moment.unix(1571140569).utc().startOf('day')).tz("America/Lima").format('LLLL');
console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data.js"></script>

moment object gives different date when formatted

formatting a moment object gives me a different result to the one I expect to see
I tried removing UTC but still don't get the result i expect
moment.utc().startOf("day").subtract(schedule.pastDays, "days")
returns date object with:
_d: Wed Jul 17 2019 00:00:00 GMT+0000 (Etc Greenwich Standard Time) {}
but formatting it:
moment.utc().startOf("day").subtract(schedule.pastDays, "days").format()
returns:
"2019-07-16T22:00:00Z"
Where did the 2hrs go that kicked the date back to the previous day?
I expected to see:
"2019-07-17T00:00:00Z" as the date object would suggest.
So if I do not specify a timezone, moment presumes utc and so adjusts when I format(). This however works and keeps the formatted time local:
var tzDay = moment().utcOffset(moment().utcOffset(), true).local()
var newDay = tzDay.format('MMDDYYYY');
console.log(newDay)
//returns today's date without any utc adjustment

moment js how to 24 hours - current time

I want to calculate 24 hours - current time so I did like this with moment.js
moment('24:00:00').subtract(now, 'HH:mm:ss').format("HH:mm:ss")
but I recive
Invalid date
Help me
I resolved it like this
moment('24:00:00', 'HH:mm:ss')
.subtract(moment().format("HH"), 'hours')
.subtract(moment().format("mm"), 'minutes')
.subtract(moment().format("ss"), 'seconds')
.format("HH:mm:ss")

Moment.js time wrong when DST is on

Don't understand why the examples on moment-timezone page give me different result than what they say they should.
Running these statements give me date with my current local time for each instance:
var jun = moment("2014-06-01T12:00:00Z");
var dec = moment("2014-12-01T12:00:00Z");
jun.tz('America/Los_Angeles').format('ha z'); // 5am PDT **I get 7am**
dec.tz('America/Los_Angeles').format('ha z'); // 4am PST **I get 6am**
What am I missing?
Found the issue was due to missing time zone data. After loading time zone data I get the expected results, below; how to load time zone data
[http://momentjs.com/timezone/docs/#/data-loading/][1]

setWeek year detection

Have a look at the year after setWeek
d = Date.today()
Wed Dec 26 2012 00:00:00 GMT+0000 (GMT Standard Time)
d.getWeek()
52
d.setWeek(52)
Mon Dec 23 2013 00:00:00 GMT+0000 (GMT Standard Time)
Anyone knows what this is about? Have I misinterpreted how the setWeek function should work?
I have found an issue in the datejs tracker with this same problem. It's from 2011, though it's still marked as new: setWeek issue
So regarding yor question of "Have I misinterpreted how the setWeek function should work?" I would say that it's a bug in the jdate code.
If you have a look at the datejs source code you will see that setWeek will always move to the Monday of the target week n:
Date.prototype.setWeek = function (n) {
return this.moveToDayOfWeek(1).addWeeks(n - this.getWeek());
};
see: datejs: svn/trunk/src/core.js

Resources