Hello i am trying to add time zone offset for start & end date.
In order to get something like this
start date: Mon May 30 2022 05:00:00 GMT+0000 here offset is (00:00 + 5 hours US central time offset)
end time: Wed Jun 01 2022 04:59:00 GMT+0000 (23:59 + 5 hours offset)
But getting for start date Mon May 29 2022 00:00:00 GMT+0000
Here is my code
export function convertIsoToMomentLocal(iso: string | ParsableDate, timezone = 'CST') {
if (iso && timezone) {
const mo = moment.tz(iso, timezone).startOf('day')
const offset = mo.utcOffset()
const epoch = mo.add(offset, 'm').unix()
console.log(epoch, 'test')
return epoch
}
}
So basically i am trying to determine start date and end date first, and only after that add time zone offset.
Any ideas tnx, i have tried everything.
Related
I am working with Date Picker component and I am trying to get selected day from date picker to be start of day (midnight) in specific timezone (e.g. Europe/London). So for example if I select 2023-01-30 I want to get something like
Mon Jan 30 2023 00:00:00 GMT (London UK)
Currently output shows my local timezone at the end GMT-0500 (Eastern Standard Time) but I would like to have London one GMT
I am using date-fns to get startOfDay
const d = '2023-01-30';
const [year, month, day] = d.split('-');
const y = startOfDay(new Date(year, month - 1, day));
console.log('🚀 ~ file: ScheduleSection.js:102 ~ convertDateToUtc ~ y', y);
Actual Output: Mon Jan 30 2023 00:00:00 GMT-0500 (Eastern Standard Time)
Expected Output: Mon Jan 30 2023 00:00:00 GMT (London UK)
Since you have tags for date-fns and date-fns-tz, you can do what you want with those libraries. The following can be run at RunKit.com:
var dateFns = require('date-fns');
var dateFnsTz = require('date-fns-tz');
let date = '2014-06-25T00:00:00';
let timeZone = 'America/Los_Angeles';
// Parse timestamp for America/Los_Angeles
let utcDate = dateFnsTz.zonedTimeToUtc(date, timeZone);
// UTC equivalent: "2014-06-25T07:00:00.000Z"
console.log(utcDate.toISOString());
// Europe/London equivalent: "2014-06-25 08:00:00 GMT+1"
console.log(dateFnsTz.formatInTimeZone(utcDate,
'Europe/London', 'yyyy-MM-dd HH:mm:ss zzz'));
As RunKit doesn't support import, require is used instead.
you can use the moment-timezone library, you can install it by running
npm install moment-timezone
in your project's root directory.
Example:
const moment = require('moment-timezone');
const d = '2023-01-30';
const date = moment.tz(d, 'Europe/London').startOf('day').format();
console.log(date);
I'm confused about how this time conversion works. I have timestamp 1462060800000 which when I turn in to date correctly becomes:
Sun May 01 2016 02:00:00 GMT+0200 (Central European Summer Time)
but then when I want to get the month with const startMonth = start.getUTCMonth() I get 4 instead of 5. Why is this happening and what do I need to do to get the correct month?
const timestamp = 1462060800000
const start = new Date(timestamp)
console.log(start) // Sun May 01 2016 02:00:00 GMT+0200 (Central European Summer Time)
const startYear = start.getUTCFullYear()
const startMonth = start.getUTCMonth()
console.log(startMonth) // 4
getUTCMonth() returns zero-based months. 4 is correct. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCMonth.
I get it it's actually a month index that starts with 0.
The getUTCMonth() returns the month of the specified date according to universal time, as a zero-based value (where zero indicates the first month of the year).
From the docs see Date.prototype.getUTCMonth()
The getUTCMonth() method, like getMonth(), has a zero (0) count. This means that the period will be like this - 0 and 11. To get the desired month, you need to add +1:
const startMonth = start.getUTCMonth() + 1;
Loot it.
const timestamp = 1462060800000;
const start = new Date(timestamp);
console.log(start); // Sun May 01 2016 02:00:00 GMT+0200 (Central European Summer Time)
const startYear = start.getUTCFullYear();
const startMonth = start.getUTCMonth() + 1;
console.log(startMonth);
I have user inputs of meeting_time which is javascript Date (calendar select) and meeting_time string (select dropdown option)
var userTime = meeting_time ? meeting_time.label.split(' -')[0] : null
var userDate = moment(meeting_date).format("YYYY-MM-DD");
How Do I converting those variable to moment.js object with specific timezone
For example,
meeting_date is Thu Jul 12 2018 12:00:00 GMT+0300 (MSK)
meeting_time is 15:00 - 15:30
I want to get moment _d Thu Jul 12 2018 20:00:00 GMT+0800 (UTC +3 to UTC +8)
You can set the hour and minute in the date.
const [hour, minute] = userTime.split(':');
moment(meeting_date).hour(hour).minute(minute).toDate();
This is how I calculate the duration:
var duration = new Date().getTime() - customerForgotPassword[0].createdTime.getTime();
This is how I compare:
var TEN_MINUTES = 10*60*1000;
if(duration > TEN_MINUTES){
//do smtg
}
new Date().getTime() returns 1528291351108 which is Wed Jun 06 2018 13:22:31 in UTC
customerForgotPassword[0].createdTime returns Wed Jun 06 2018 13:20:04 GMT+0800 (Malay Peninsula Standard Time) in my code.
customerForgotPassword[0].createdTime.getTime() returns 1528262404000 which is Wed Jun 06 2018 05:20:04 in UTC
In database, customerForgotPassword[0].createdTime is in UTC format but when I retrieve it, it shows the local timezone. How can I set this to UTC format too when I retrieve it in node.js using Sequelize ?
I had to add 8 hours to get time in UTC
var timeInUTC = customerForgotPassword[0].createdTime.getTime() - (customerForgotPassword[0].createdTime.getTimezoneOffset() * 60 * 1000)
I need to store an unix value from a Time input.
The problem is that :
// I create a Moment Date from my input :
var date = moment({hour: 10, minute: 00)
// gives this _d : Mon May 04 2015 10:00:00 GMT+0200 (CEST)
// I convert it to unix value
date = date.unix()
// -> 1430726400
moment( date ).format('HH:mm')
// -> "14:25" // Should give me 10:00
// Online conversion unix time gives me : Mon, 04 May 2015 08:00:00 GMT
So how can I keep my 10:00 in memory as unix value using those transformations ?
Per documentation:
moment.unix( date ).format('HH:mm')
I kind of sorted it this way :
// I create a Moment Date from my input :
var date = moment({hour: 10, minute: 00)
// gives this _d : Mon May 04 2015 10:00:00 GMT+0200 (CEST)
// Convert to unix
date = date.toDate();
date = date.getTime();
console.log(moment( date ).format('HH:mm')); // Gives me 10:00
I hope it will keep the good time ? In France the time changes twice a year ( go forward 1 hour, go backwards 1 hour)