Why startOfMonth results in different timezone that endOfMonth - javascript

I am trying to get proper start and end of month values using date-fns. My browser timezone is UTC+2 (as new Date().getTimezoneOffset() results in -120).
Example code:
console.log('start of month: ', dateFns.startOfMonth(new Date()));
console.log('end of month: ', dateFns.endOfMonth(new Date()));
result:
start of month: Thu Oct 01 2020 00:00:00 GMT+0200 (czas środkowoeuropejski letni)
end of month: Sat Oct 31 2020 23:59:59 GMT+0100 (czas środkowoeuropejski standardowy)
Why startOfMonth results in timezone GMT+0200 while endOfMonth results in GMT+1? Is it possible to get proper values of timezones (GMT+0200) for both cases?

Related

new Date() render the wrong date format

i am struggling with my code, new Date() convert my value to next day 1 hour
new Date('2013-03-27T23:59:59.999Z') // => Thu Mar 28 2013 00:59:59 GMT+0100
As a Solution:
new Date('2013-03-27T23:59:59.999Z'.replace(/-/g, '\/').replace(/T.+/, '')) // => Wed Mar 27 2013 00:00:00 GMT+0100
Any suggestions how to get the correct date?

How to set start day time to particular time in momentjs

I'm trying to set the start time of the day to a particular time. currently, in momentjs, I can get startOf day like this
let now = moment()
console.log('now', now.toString())
console.log('start Day', now.startOf('day').toString()) // Thu Oct 07 2021 00:00:00 GMT+0530
console.log('end day', now.endOf('day').toString()) //Thu Oct 07 2021 23:59:59 GMT+0530
is there any way so I can set my day start from particular time like I want to start my day from
Thu Oct 07 2021 08:00:00 GMT+0530
and end on
Thu Oct 07 2021 07:59:59 GMT+0530
You should probably write your own function in order to achieve this.
function customStartOf(momentObj) {
return momentObj.clone().startOf('day').hours(8);
}
function customEndOf(momentObj) {
// I assume that end of the day is bigger than start of the day
return momentObj.clone().endOf('day').add(1, 'days').hours(7);
}
let now = moment();
console.log('now', now.toString()) ;
console.log('start Day', now.startOf('day').toString());
console.log('end day', now.endOf('day').toString());
console.log('custom start Day', customStartOf(now).toString());
console.log('custom end day', customEndOf(now).toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
You have to take into account the timezone.
let myDate = new Date();
const timezoneOffset = moment(myDate).utcOffset();
moment(myDate).utc().add(timezoneOffset, 'minutes').startOf('day').format();
moment(myDate).utc().add(timezoneOffset, 'minutes').endOf('day').format();

javascript Date.toISOString() return difference date value

I'm confusing about the javascript Date.toISOString() function which shown as below example, how come date value of x in ISO format become January?
const date = new Date();
const x = (new Date(date.getFullYear(), date.getMonth() , 1));
console.log(date); \\Tue Feb 04 2020 11:11:12 GMT+0800 (Malaysia Time)
console.log(x); \\Sat Feb 01 2020 00:00:00 GMT+0800 (Malaysia Time)
console.log(date.toISOString()); \\2020-02-04T03:11:12.330Z
console.log(x.toISOString()); \\2020-01-31T16:00:00.000Z
This is due to time zone conversion from GMT+08 to UTC. The toISOString function converts the date to UTC (as a note you can determine that the date is in the UTC time zone by "Z" at the end of the string).
When converting Feb 01 2020 00:00:00 GMT+0800 to an ISO string, the date is reduced by 8 hours and hence becomes Jan 31 2020 16:00:00.

Why .setUTCHours returns different results?

I have the following function which should read time in UTC and return in local time, and sometimes it adds 2 hours(correctly) and sometimes 4 hours. Why is that? What can be the reason?
value = time in UTC
toLocalDate(value) {
let string = new Date(value);
let date = new Date();
date.setUTCFullYear(string.getFullYear(), string.getMonth(), string.getDate());
date.setUTCHours(string.getHours(), string.getMinutes(), string.getSeconds(), 0);
return date;
}
Example data:
Value: 2017-08-23T06:00:00
Expected output: Wed Aug 23 2017 08:00:00 GMT+0200 (Central European Daylight Time)
Output: Wed Aug 23 2017 10:00:00 GMT+0200 (Central European Daylight Time)
On some devices(like my phone or computer) it returns the expected output.
On my friend's device(mobile) it returns the second output.

How to format date without time using toLocaleTimeString

I have a Date, which I want to convert to a string, but without the time, just the date part.
My current code:
var date = new Date(2014, 9, 08); //Wed Oct 08 2014 00:00:00 GMT-0500 (Central Daylight Time)
var options = {weekday: "long", year: "numeric", month: "long", day: "numeric"};
console.log(date.toLocaleTimeString("en-US", options));
// output: Wednesday, October 8, 2014 12:00:00 AM
// what I'm looking for: Wednesday, October 8, 2014
How can I modify the options to not display time?
Juste use toLocaleDateString instead of toLocaleTimeString and you should get the result you are expecting :
var date = new Date('2014', '9', '08'); //Wed Oct 08 2014 00:00:00 GMT-0500 (Central Daylight Time)
var options = {weekday: "long", year: "numeric", month: "long", day: "numeric"};
console.log(date.toLocaleDateString("en-US", options));
returns : "Wednesday, October 8, 2014"
I will also second the above poster and recommend using moment.js; it is lightweight and very flexible.
If you are satisfied with allowing the browser to determine the display format, you can use the toDateString or toLocaleDateString functions of the Date object. You will get different results on different browsers.
Otherwise, consider using a library such as moment.js, so you can control the format to your liking. For example:
moment([2014,9,8]).format('dddd, MMMM Do, YYYY') // "Wednesday, October 8, 2014"

Categories

Resources