Javascript : evaluate delta between UTC and Central Europe Time2019 Community Moderator ElectionConvert UTC/GMT time to local time“date_part('epoch', now() at time zone 'UTC')” not the same time as “now() at time zone 'UTC'” in postgresqlHow to convert time correctly across timezones?R - Reading ISO8601 formatWhy are timezones with same offset from UTC are showing different times?Getting a date and time input in Eastern Time and converting to UTC timestamp in JavaConverting UTC to local time doesn't work in javascriptJava Calendar and Time UTC to BSTBehaviour of GregorianCalendar.set() with daylight saving timesCreate time entry in Firestore with UTC 0
What has been your most complicated TikZ drawing?
Do Bugbears' arms literally get longer when it's their turn?
Time dilation for a moving electronic clock
Rejected in 4th interview round citing insufficient years of experience
What is the dot in “1.2.4."
Time travel short story where dinosaur doesn't taste like chicken
What does it mean when multiple 々 marks follow a 、?
Playing ONE triplet (not three)
Is all copper pipe pretty much the same?
Do I need to leave some extra space available on the disk which my database log files reside, for log backup operations to successfully occur?
Is it illegal in Germany to take sick leave if you caused your own illness with food?
When were linguistics departments first established
Best approach to update all entries in a list that is paginated?
Is having access to past exams cheating and, if yes, could it be proven just by a good grade?
Co-worker team leader wants to inject the crap software product of his friends into our development. What should I say to our common boss?
Can infringement of a trademark be pursued for using a company's name in a sentence?
If Invisibility ends because the original caster casts a non-concentration spell, does Invisibility also end on other targets of the original casting?
Ban on all campaign finance?
Why would a jet engine that runs at temps excess of 2000°C burn when it crashes?
Making a sword in the stone, in a medieval world without magic
When two POV characters meet
What is the difference between "shut" and "close"?
Is going from continuous data to categorical always wrong?
redhat 7 + How to stop systemctl service permanent
Javascript : evaluate delta between UTC and Central Europe Time
2019 Community Moderator ElectionConvert UTC/GMT time to local time“date_part('epoch', now() at time zone 'UTC')” not the same time as “now() at time zone 'UTC'” in postgresqlHow to convert time correctly across timezones?R - Reading ISO8601 formatWhy are timezones with same offset from UTC are showing different times?Getting a date and time input in Eastern Time and converting to UTC timestamp in JavaConverting UTC to local time doesn't work in javascriptJava Calendar and Time UTC to BSTBehaviour of GregorianCalendar.set() with daylight saving timesCreate time entry in Firestore with UTC 0
I have a server app in Firebase cloud function with webhooks that receive data from a third party API.
In those data, I have times expressed in HH:mm local time (Central Europe Time).
I need to store them in Firestore as a timestamp UTC.
I tried this:
//Evaluate offset between CET and UTC
const dAujourdhui = new Date(); //07/03/2019 10:39 (UTC)
const nHeureUTC = dAujourdhui.getUTCHours(); //10
const nHeureLocal = dAujourdhui.getHours(); //10
const timeZoneOffset = nHeureUTC - nHeureLocal //0!
// Build a timestamp based on today's date and received time
const currentDateString = dateFormat(dAujourdhui, "yyyy-mm-dd");
var estimatedDateTimeOfArrival = new Date(`$currentDateString $timeReceivedFrom3rdPartyAPI`);
// Add offset to hours
const utcHour = estimatedDateTimeOfArrival.getUTCHours()
estimatedDateTimeOfArrival.setHours(utcHour + timeZoneOffset)
// Save to firestore
...
As you can see, I'm not able to evaluate the offset between CET and UTC as the code is run in a server set in UTC.
Any idea on how to solve it ?
node.js datetime google-cloud-functions timezone-offset
add a comment |
I have a server app in Firebase cloud function with webhooks that receive data from a third party API.
In those data, I have times expressed in HH:mm local time (Central Europe Time).
I need to store them in Firestore as a timestamp UTC.
I tried this:
//Evaluate offset between CET and UTC
const dAujourdhui = new Date(); //07/03/2019 10:39 (UTC)
const nHeureUTC = dAujourdhui.getUTCHours(); //10
const nHeureLocal = dAujourdhui.getHours(); //10
const timeZoneOffset = nHeureUTC - nHeureLocal //0!
// Build a timestamp based on today's date and received time
const currentDateString = dateFormat(dAujourdhui, "yyyy-mm-dd");
var estimatedDateTimeOfArrival = new Date(`$currentDateString $timeReceivedFrom3rdPartyAPI`);
// Add offset to hours
const utcHour = estimatedDateTimeOfArrival.getUTCHours()
estimatedDateTimeOfArrival.setHours(utcHour + timeZoneOffset)
// Save to firestore
...
As you can see, I'm not able to evaluate the offset between CET and UTC as the code is run in a server set in UTC.
Any idea on how to solve it ?
node.js datetime google-cloud-functions timezone-offset
What exactly do you receive? Please provide an example. Also, you have adateFormat
function - is that coming from a library? Which one? Lastly, when you save to firestore at the end, what are you saving? Do you pass aDate
object or a string in a particular format to some API? which one? As you can see, without a Minimal, Complete, and Verifiable Example, it's difficult to provide assistance.
– Matt Johnson
Mar 7 at 18:41
1
A couple things from what you provided: 1) When you add the offset to the hour, you're not adjusting for time zones but rather you're picking a different point in time. 2) JavaScript already has agetTimezoneOffset
function that does what that first block of code does, but that won't help you here. 3) The offset from UTC may be different depending on the date you apply it to, such as when summer time is in effect. 4) You may end up needing a library such as Luxon or Moment-Timezone, depending on your input string.
– Matt Johnson
Mar 7 at 18:46
Thanks Matt Johnson. Indeed, I figured out that I need to go for an external library. I have been able to solve the problem with 'moment-timezone'
– Laurent Maquet
Mar 8 at 8:42
add a comment |
I have a server app in Firebase cloud function with webhooks that receive data from a third party API.
In those data, I have times expressed in HH:mm local time (Central Europe Time).
I need to store them in Firestore as a timestamp UTC.
I tried this:
//Evaluate offset between CET and UTC
const dAujourdhui = new Date(); //07/03/2019 10:39 (UTC)
const nHeureUTC = dAujourdhui.getUTCHours(); //10
const nHeureLocal = dAujourdhui.getHours(); //10
const timeZoneOffset = nHeureUTC - nHeureLocal //0!
// Build a timestamp based on today's date and received time
const currentDateString = dateFormat(dAujourdhui, "yyyy-mm-dd");
var estimatedDateTimeOfArrival = new Date(`$currentDateString $timeReceivedFrom3rdPartyAPI`);
// Add offset to hours
const utcHour = estimatedDateTimeOfArrival.getUTCHours()
estimatedDateTimeOfArrival.setHours(utcHour + timeZoneOffset)
// Save to firestore
...
As you can see, I'm not able to evaluate the offset between CET and UTC as the code is run in a server set in UTC.
Any idea on how to solve it ?
node.js datetime google-cloud-functions timezone-offset
I have a server app in Firebase cloud function with webhooks that receive data from a third party API.
In those data, I have times expressed in HH:mm local time (Central Europe Time).
I need to store them in Firestore as a timestamp UTC.
I tried this:
//Evaluate offset between CET and UTC
const dAujourdhui = new Date(); //07/03/2019 10:39 (UTC)
const nHeureUTC = dAujourdhui.getUTCHours(); //10
const nHeureLocal = dAujourdhui.getHours(); //10
const timeZoneOffset = nHeureUTC - nHeureLocal //0!
// Build a timestamp based on today's date and received time
const currentDateString = dateFormat(dAujourdhui, "yyyy-mm-dd");
var estimatedDateTimeOfArrival = new Date(`$currentDateString $timeReceivedFrom3rdPartyAPI`);
// Add offset to hours
const utcHour = estimatedDateTimeOfArrival.getUTCHours()
estimatedDateTimeOfArrival.setHours(utcHour + timeZoneOffset)
// Save to firestore
...
As you can see, I'm not able to evaluate the offset between CET and UTC as the code is run in a server set in UTC.
Any idea on how to solve it ?
node.js datetime google-cloud-functions timezone-offset
node.js datetime google-cloud-functions timezone-offset
asked Mar 7 at 11:02
Laurent MaquetLaurent Maquet
1489
1489
What exactly do you receive? Please provide an example. Also, you have adateFormat
function - is that coming from a library? Which one? Lastly, when you save to firestore at the end, what are you saving? Do you pass aDate
object or a string in a particular format to some API? which one? As you can see, without a Minimal, Complete, and Verifiable Example, it's difficult to provide assistance.
– Matt Johnson
Mar 7 at 18:41
1
A couple things from what you provided: 1) When you add the offset to the hour, you're not adjusting for time zones but rather you're picking a different point in time. 2) JavaScript already has agetTimezoneOffset
function that does what that first block of code does, but that won't help you here. 3) The offset from UTC may be different depending on the date you apply it to, such as when summer time is in effect. 4) You may end up needing a library such as Luxon or Moment-Timezone, depending on your input string.
– Matt Johnson
Mar 7 at 18:46
Thanks Matt Johnson. Indeed, I figured out that I need to go for an external library. I have been able to solve the problem with 'moment-timezone'
– Laurent Maquet
Mar 8 at 8:42
add a comment |
What exactly do you receive? Please provide an example. Also, you have adateFormat
function - is that coming from a library? Which one? Lastly, when you save to firestore at the end, what are you saving? Do you pass aDate
object or a string in a particular format to some API? which one? As you can see, without a Minimal, Complete, and Verifiable Example, it's difficult to provide assistance.
– Matt Johnson
Mar 7 at 18:41
1
A couple things from what you provided: 1) When you add the offset to the hour, you're not adjusting for time zones but rather you're picking a different point in time. 2) JavaScript already has agetTimezoneOffset
function that does what that first block of code does, but that won't help you here. 3) The offset from UTC may be different depending on the date you apply it to, such as when summer time is in effect. 4) You may end up needing a library such as Luxon or Moment-Timezone, depending on your input string.
– Matt Johnson
Mar 7 at 18:46
Thanks Matt Johnson. Indeed, I figured out that I need to go for an external library. I have been able to solve the problem with 'moment-timezone'
– Laurent Maquet
Mar 8 at 8:42
What exactly do you receive? Please provide an example. Also, you have a
dateFormat
function - is that coming from a library? Which one? Lastly, when you save to firestore at the end, what are you saving? Do you pass a Date
object or a string in a particular format to some API? which one? As you can see, without a Minimal, Complete, and Verifiable Example, it's difficult to provide assistance.– Matt Johnson
Mar 7 at 18:41
What exactly do you receive? Please provide an example. Also, you have a
dateFormat
function - is that coming from a library? Which one? Lastly, when you save to firestore at the end, what are you saving? Do you pass a Date
object or a string in a particular format to some API? which one? As you can see, without a Minimal, Complete, and Verifiable Example, it's difficult to provide assistance.– Matt Johnson
Mar 7 at 18:41
1
1
A couple things from what you provided: 1) When you add the offset to the hour, you're not adjusting for time zones but rather you're picking a different point in time. 2) JavaScript already has a
getTimezoneOffset
function that does what that first block of code does, but that won't help you here. 3) The offset from UTC may be different depending on the date you apply it to, such as when summer time is in effect. 4) You may end up needing a library such as Luxon or Moment-Timezone, depending on your input string.– Matt Johnson
Mar 7 at 18:46
A couple things from what you provided: 1) When you add the offset to the hour, you're not adjusting for time zones but rather you're picking a different point in time. 2) JavaScript already has a
getTimezoneOffset
function that does what that first block of code does, but that won't help you here. 3) The offset from UTC may be different depending on the date you apply it to, such as when summer time is in effect. 4) You may end up needing a library such as Luxon or Moment-Timezone, depending on your input string.– Matt Johnson
Mar 7 at 18:46
Thanks Matt Johnson. Indeed, I figured out that I need to go for an external library. I have been able to solve the problem with 'moment-timezone'
– Laurent Maquet
Mar 8 at 8:42
Thanks Matt Johnson. Indeed, I figured out that I need to go for an external library. I have been able to solve the problem with 'moment-timezone'
– Laurent Maquet
Mar 8 at 8:42
add a comment |
1 Answer
1
active
oldest
votes
Here is a solution I found to solve the problem.
It is based on an external library: 'moment-timezone'
// Example's initial setup
const dateFormat = require('dateformat')
const moment = require('moment-timezone');
const timeReceivedFrom3rdPartyAPI = "14:30" // Format "HH:mm", Paris time
// Construct Date object with today's date and timeReceivedFrom3rdPartyAPI
const currentDate = new Date();
const currentDateString = dateFormat(currentDate, "yyyy-mm-dd");
const estimatedDateTimeOfArrival = moment.tz(`$currentDateString $timeReceivedFrom3rdPartyAPI`, "Europe/Paris").toDate()
console.log(estimatedDateTimeOfArrival)
// 08/03/2018 13:30 UTC
// Now I have UTC Full Date corresponding to timeReceivedFrom3rdPartyAPI
Don't tack on the date. You're using the current local date, which may or may not be the same as the current date in the given time zone. Just let Moment do the work:moment.tz('14:30', 'HH:mm', 'Europe/Paris')
. Also, unless you need the output to be aDate
object, you're better off calling.toISOString()
, or.utc().format()
. (The latter would allow you to pass a format string if you want a specific format).
– Matt Johnson
Mar 8 at 17:08
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55042255%2fjavascript-evaluate-delta-between-utc-and-central-europe-time%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is a solution I found to solve the problem.
It is based on an external library: 'moment-timezone'
// Example's initial setup
const dateFormat = require('dateformat')
const moment = require('moment-timezone');
const timeReceivedFrom3rdPartyAPI = "14:30" // Format "HH:mm", Paris time
// Construct Date object with today's date and timeReceivedFrom3rdPartyAPI
const currentDate = new Date();
const currentDateString = dateFormat(currentDate, "yyyy-mm-dd");
const estimatedDateTimeOfArrival = moment.tz(`$currentDateString $timeReceivedFrom3rdPartyAPI`, "Europe/Paris").toDate()
console.log(estimatedDateTimeOfArrival)
// 08/03/2018 13:30 UTC
// Now I have UTC Full Date corresponding to timeReceivedFrom3rdPartyAPI
Don't tack on the date. You're using the current local date, which may or may not be the same as the current date in the given time zone. Just let Moment do the work:moment.tz('14:30', 'HH:mm', 'Europe/Paris')
. Also, unless you need the output to be aDate
object, you're better off calling.toISOString()
, or.utc().format()
. (The latter would allow you to pass a format string if you want a specific format).
– Matt Johnson
Mar 8 at 17:08
add a comment |
Here is a solution I found to solve the problem.
It is based on an external library: 'moment-timezone'
// Example's initial setup
const dateFormat = require('dateformat')
const moment = require('moment-timezone');
const timeReceivedFrom3rdPartyAPI = "14:30" // Format "HH:mm", Paris time
// Construct Date object with today's date and timeReceivedFrom3rdPartyAPI
const currentDate = new Date();
const currentDateString = dateFormat(currentDate, "yyyy-mm-dd");
const estimatedDateTimeOfArrival = moment.tz(`$currentDateString $timeReceivedFrom3rdPartyAPI`, "Europe/Paris").toDate()
console.log(estimatedDateTimeOfArrival)
// 08/03/2018 13:30 UTC
// Now I have UTC Full Date corresponding to timeReceivedFrom3rdPartyAPI
Don't tack on the date. You're using the current local date, which may or may not be the same as the current date in the given time zone. Just let Moment do the work:moment.tz('14:30', 'HH:mm', 'Europe/Paris')
. Also, unless you need the output to be aDate
object, you're better off calling.toISOString()
, or.utc().format()
. (The latter would allow you to pass a format string if you want a specific format).
– Matt Johnson
Mar 8 at 17:08
add a comment |
Here is a solution I found to solve the problem.
It is based on an external library: 'moment-timezone'
// Example's initial setup
const dateFormat = require('dateformat')
const moment = require('moment-timezone');
const timeReceivedFrom3rdPartyAPI = "14:30" // Format "HH:mm", Paris time
// Construct Date object with today's date and timeReceivedFrom3rdPartyAPI
const currentDate = new Date();
const currentDateString = dateFormat(currentDate, "yyyy-mm-dd");
const estimatedDateTimeOfArrival = moment.tz(`$currentDateString $timeReceivedFrom3rdPartyAPI`, "Europe/Paris").toDate()
console.log(estimatedDateTimeOfArrival)
// 08/03/2018 13:30 UTC
// Now I have UTC Full Date corresponding to timeReceivedFrom3rdPartyAPI
Here is a solution I found to solve the problem.
It is based on an external library: 'moment-timezone'
// Example's initial setup
const dateFormat = require('dateformat')
const moment = require('moment-timezone');
const timeReceivedFrom3rdPartyAPI = "14:30" // Format "HH:mm", Paris time
// Construct Date object with today's date and timeReceivedFrom3rdPartyAPI
const currentDate = new Date();
const currentDateString = dateFormat(currentDate, "yyyy-mm-dd");
const estimatedDateTimeOfArrival = moment.tz(`$currentDateString $timeReceivedFrom3rdPartyAPI`, "Europe/Paris").toDate()
console.log(estimatedDateTimeOfArrival)
// 08/03/2018 13:30 UTC
// Now I have UTC Full Date corresponding to timeReceivedFrom3rdPartyAPI
answered Mar 8 at 8:56
Laurent MaquetLaurent Maquet
1489
1489
Don't tack on the date. You're using the current local date, which may or may not be the same as the current date in the given time zone. Just let Moment do the work:moment.tz('14:30', 'HH:mm', 'Europe/Paris')
. Also, unless you need the output to be aDate
object, you're better off calling.toISOString()
, or.utc().format()
. (The latter would allow you to pass a format string if you want a specific format).
– Matt Johnson
Mar 8 at 17:08
add a comment |
Don't tack on the date. You're using the current local date, which may or may not be the same as the current date in the given time zone. Just let Moment do the work:moment.tz('14:30', 'HH:mm', 'Europe/Paris')
. Also, unless you need the output to be aDate
object, you're better off calling.toISOString()
, or.utc().format()
. (The latter would allow you to pass a format string if you want a specific format).
– Matt Johnson
Mar 8 at 17:08
Don't tack on the date. You're using the current local date, which may or may not be the same as the current date in the given time zone. Just let Moment do the work:
moment.tz('14:30', 'HH:mm', 'Europe/Paris')
. Also, unless you need the output to be a Date
object, you're better off calling .toISOString()
, or .utc().format()
. (The latter would allow you to pass a format string if you want a specific format).– Matt Johnson
Mar 8 at 17:08
Don't tack on the date. You're using the current local date, which may or may not be the same as the current date in the given time zone. Just let Moment do the work:
moment.tz('14:30', 'HH:mm', 'Europe/Paris')
. Also, unless you need the output to be a Date
object, you're better off calling .toISOString()
, or .utc().format()
. (The latter would allow you to pass a format string if you want a specific format).– Matt Johnson
Mar 8 at 17:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55042255%2fjavascript-evaluate-delta-between-utc-and-central-europe-time%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What exactly do you receive? Please provide an example. Also, you have a
dateFormat
function - is that coming from a library? Which one? Lastly, when you save to firestore at the end, what are you saving? Do you pass aDate
object or a string in a particular format to some API? which one? As you can see, without a Minimal, Complete, and Verifiable Example, it's difficult to provide assistance.– Matt Johnson
Mar 7 at 18:41
1
A couple things from what you provided: 1) When you add the offset to the hour, you're not adjusting for time zones but rather you're picking a different point in time. 2) JavaScript already has a
getTimezoneOffset
function that does what that first block of code does, but that won't help you here. 3) The offset from UTC may be different depending on the date you apply it to, such as when summer time is in effect. 4) You may end up needing a library such as Luxon or Moment-Timezone, depending on your input string.– Matt Johnson
Mar 7 at 18:46
Thanks Matt Johnson. Indeed, I figured out that I need to go for an external library. I have been able to solve the problem with 'moment-timezone'
– Laurent Maquet
Mar 8 at 8:42