Working with dates and timezones

https://www.reddit.com/r/ProgrammerHumor/comments/gepnom/if_youve_never_worked_with_time_zones_you_dont/

Why?

  • JavaScript always considers dates to be particular moments in time.
  • They’re stored as UTC but most methods automatically localize for you.
  • Not every date is really something that happened at a particular moment in time.
  • In these cases, the JavaScript Date object often supplies too much or erroneous information

Solution

const localOffset = new Date().getTimezoneOffset();
const clientOffset = new Date().getTimezoneOffset(); 
const clientDate = from_client;
const serverOffset = new Date().getTimezoneOffset();
const minutesDiff = serverOffset - clientOffset;
if (minutesDiff !== 0) {
if (direction === 'CLIENT_TO_SERVER') {
clientDate = moment(clientDate).subtract(minutesDiff,'minutes').toDate();
} else {
clientDate = moment(clientDate).add(minutesDiff,'minutes').toDate();
}
}

--

--

--

Developer , Pentester

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

An Introduction to the DOM

Javascript- What happens to our code?

Next.js firebase, client side render

Vue vs Angular — A comprehensive comparison

Automatically set cookies from Back-end in React

React, Node, Express and Let’s Encrypt: Run an HTTPS server for your project

Find the real IP of the user if she has used VPN with js+php

Some common and important JavaScript related Interview questions

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hasky

Hasky

Developer , Pentester

More from Medium

4 Decision Making Styles: Who Are You?

Shattered Dreams: Part One

It’s a Twin Thing

Sweat is more than salt water — unknown patches