How to set start day time to particular time in momentjs - javascript

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();

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?

Why startOfMonth results in different timezone that endOfMonth

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?

Weird date issue in javascript

I have a SharePoint list with delivery date column. I have created a table to display current week and next week delivery items using javascript. Everything works fine but for couple of team members Thursday delivery items are displaying in Wednesday cell and Friday items in Thursday cell.
I am not sure why it is happening. Can anyone help me out to resolve this issue? Any help would be greatly appreciated.
Here is my code. Added alerts to verify data, I am wondering why in second and third alerts sunday is showing Monday date and in third alert monday is showing Tuesday date. Added alert messages at the bottom. Please advice.
today = moment();
sundayDate = new Date(today.startOf('week'));
sundayShortDate = sundayDate.toLocaleDateString();
sundayTitle = getFormattedDate(sundayDate);
window.alert("sundayDate ::"+sundayDate+"");
monDate = new Date(sundayDate.setDate(sundayDate.getDate() + 1));
monSDate = monDate.toLocaleDateString();
monTitle = getFormattedDate(monDate);
window.alert("sundayDate ::"+sundayDate+"; monDate::"+monDate+"");
tueDate = new Date(monDate.setDate(monDate.getDate() + 1));
tuesSDate = tueDate.toLocaleDateString();
tueTitle = getFormattedDate(tueDate);
window.alert("sundayDate ::"+sundayDate+"; monDate::"+monDate+"; tueDate::"+tueDate+"");
First window alert:
sundayDate ::Sun Aug 16 2020 00:00:00 GMT-0400 (Eastern Daylight Time)
Second Alert:
sundayDate ::Mon Aug 17 2020 00:00:00 GMT-0400 (Eastern Daylight Time); monDate::Mon Aug 17 2020 00:00:00 GMT-0400 (Eastern Daylight Time)
Third Alert:
sundayDate ::Mon Aug 17 2020 00:00:00 GMT-0400 (Eastern Daylight Time); monDate::Tue Aug 18 2020 00:00:00 GMT-0400 (Eastern Daylight Time); tueDate::Tue Aug 18 2020 00:00:00 GMT-0400 (Eastern Daylight Time)
As was already commented, you are mutating the dates with calls to setDate().
As you already use moment, don't switch back to Date objects, but stick to moment objects.
For example:
let today = moment();
let fmt = "dddd, MMMM Do YYYY";
let sunDate = today.startOf('week');
let sunShortDate = sunDate.format(fmt);
let monDate = sunDate.clone().add(1, "day");
let monShortDate = monDate.format(fmt);
let tueDate = monDate.clone().add(1, "day");
let tueShortDate = tueDate.format(fmt);
console.log({ sunShortDate, monShortDate, tueShortDate });
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.0/moment.min.js"></script>
Just like Date objects, also moment objects are mutable, and so you should call clone first before calling add.
You set sundayDate to sundayDate.setDate(sundayDate.getDate() + 1)
You should do:
monDate = new Date(sundayDate.getDate() + 1);
P.S.
I'm not sure if mixing moment.js and js Date is a good idea.

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.

Converting a date string into UTC+0530 format using javascript

I have a date in the format 14-Feb-2011, but I want to convert it into the format Mon Feb 14 10:13:50 UTC+0530 2011. How Can I achieve this?
Using new Date(Date.UTC(year, month, day, hour, minute, second)) you can create a Date-object from a specific UTC time.
I tried this code and it returned proper date (In Indian Locale)
var d=Date.parse("14,Feb,2011");
document.write(new Date(d));
Output:
Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time) .
Here's an example of converting between different time zones.
<html>
<body>
<script type="text/javascript">
//Set you offset here like +5.5 for IST
var offsetIST = 5.5;
//Set you offset here like -8 for PST
var offsetPST = -8;
//Create a new date from the Given string
var d=new Date(Date.parse("14,Feb,2011"));
//To convert to UTC datetime by subtracting the current Timezone offset
var utcdate = new Date(d.getTime() + (d.getTimezoneOffset()*60000));
//Then cinver the UTS date to the required time zone offset like back to 5.5 for IST
var istdate = new Date(utcdate.getTime() - ((-offsetIST*60)*60000));
//Then cinver the UTS date to the required time zone offset like back to -8 for PST (Canada US)
var pstdate= new Date(utcdate.getTime() - ((-offsetPST*60)*60000));
document.write(d);
document.write("<br/>");
document.write(utcdate);
document.write("<br/>");
document.write(istdate);
document.write("<br/>");
document.write(pstdate);
</script>
</body>
</html>
Output:
Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time)
Sun Feb 13 2011 18:30:00 GMT+0530 (India Standard Time)
Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time)
Sun Feb 13 2011 10:30:00 GMT+0530 (India Standard Time)
Its writing IST every where because new Date() always show date as local timezone (which is IST for me) but above datetime are actually Original, UTC, IST, PST respectively.
var d = new Date("14-Feb-2011");
this will give an output of
Mon Feb 14 2011 00:00:00 GMT-0500 (Eastern Standard Time)

Categories

Resources