Skip to main content
The 2024 Developer Survey results are live! See the results
added 106 characters in body
Source Link

Get time offset (minutes) of timezone name

function getOffset(zoneName) {
  let parts=new Intl.DateTimeFormat(
    "en",{
      timeZone:zoneName,hour12:false,day:"numeric",
      hour:"numeric",minute:"numeric"
      }
    ).formatToParts(new Date("2024-01-02T12:00Z"));
  //change the month (01) according to your DST needs
  let d=parts.find(e=>e.type=="day").value;
  let h=parts.find(e=>e.type=="hour").value;
  let n=parts.find(e=>e.type=="minute").value;
  return (n-0)+(h*60)+(d*60*24)-60*12-60*24*2;
  };

You can adjust the year and month (and partially the time as well) at will, but the day must be >1 and <the last day in the month so that the month stays the same after the time shift due to this kind of calculation.

Alternatively, you can reparse the shifted values as a new Date in UTC and get the difference. Then you can use an arbitrary date (and time):

function getOffset(date,zoneName) {
  //date as Date object 
    //Date.UTC()
    //new Date(ms_timestamp)
    //Date.now()
  //or "yyyy-mm-ddThh:nn:ssZ"
  let parts=new Intl.DateTimeFormat(
    "en",{
      timeZone:zoneName,hour12:false,
      year:"numeric",month:"2-digit,"day:"2-digit",
      hour:"2-digit",minute:"2-digit",second:"2-digit",
      fractionalSecondDigits:3
      }
    ).formatToParts(new Date(date));
  let y=parts.find(e=>e.type=="year").value;
  let m=parts.find(e=>e.type=="month").value;
  let d=parts.find(e=>e.type=="day").value;
  let h=parts.find(e=>e.type=="hour").value;
  let n=parts.find(e=>e.type=="minute").value;
  let s=parts.find(e=>e.type=="second").valuevalue;
  let f=parts.find(e=>e.type=="fractionalSecond").value;
  let date2=new Date(y+"-"+m+"-"+d+"T"+h+":"+n+":"+s+"Z""+s+"."+f+"Z");
  return (date2.getTime()-date.getTime())/1000/60
  };

Eventually, you will have to put it in a try catch block.

Get time offset (minutes) of timezone name

function getOffset(zoneName) {
  let parts=new Intl.DateTimeFormat(
    "en",{
      timeZone:zoneName,hour12:false,day:"numeric",
      hour:"numeric",minute:"numeric"
      }
    ).formatToParts(new Date("2024-01-02T12:00Z"));
  //change the month (01) according to your DST needs
  let d=parts.find(e=>e.type=="day").value;
  let h=parts.find(e=>e.type=="hour").value;
  let n=parts.find(e=>e.type=="minute").value;
  return (n-0)+(h*60)+(d*60*24)-60*12-60*24*2;
  };

You can adjust the year and month (and partially the time as well) at will, but the day must be >1 and <the last day in the month so that the month stays the same after the time shift due to this kind of calculation.

Alternatively, you can reparse the shifted values as a new Date in UTC and get the difference. Then you can use an arbitrary date (and time):

function getOffset(date,zoneName) {
  //date as Date object 
    //Date.UTC()
    //new Date(ms_timestamp)
    //Date.now()
  //or "yyyy-mm-ddThh:nn:ssZ"
  let parts=new Intl.DateTimeFormat(
    "en",{
      timeZone:zoneName,hour12:false,
      year:"numeric",month:"2-digit,"day:"2-digit",
      hour:"2-digit",minute:"2-digit",second:"2-digit"
      }
    ).formatToParts(new Date(date));
  let y=parts.find(e=>e.type=="year").value;
  let m=parts.find(e=>e.type=="month").value;
  let d=parts.find(e=>e.type=="day").value;
  let h=parts.find(e=>e.type=="hour").value;
  let n=parts.find(e=>e.type=="minute").value;
  let s=parts.find(e=>e.type=="second").value
  let date2=new Date(y+"-"+m+"-"+d+"T"+h+":"+n+":"+s+"Z");
  return (date2.getTime()-date.getTime())/1000/60
  };

Eventually, you will have to put it in a try catch block.

Get time offset (minutes) of timezone name

function getOffset(zoneName) {
  let parts=new Intl.DateTimeFormat(
    "en",{
      timeZone:zoneName,hour12:false,day:"numeric",
      hour:"numeric",minute:"numeric"
      }
    ).formatToParts(new Date("2024-01-02T12:00Z"));
  //change the month (01) according to your DST needs
  let d=parts.find(e=>e.type=="day").value;
  let h=parts.find(e=>e.type=="hour").value;
  let n=parts.find(e=>e.type=="minute").value;
  return (n-0)+(h*60)+(d*60*24)-60*12-60*24*2;
  };

You can adjust the year and month (and partially the time as well) at will, but the day must be >1 and <the last day in the month so that the month stays the same after the time shift due to this kind of calculation.

Alternatively, you can reparse the shifted values as a new Date in UTC and get the difference. Then you can use an arbitrary date (and time):

function getOffset(date,zoneName) {
  //date as Date object 
    //Date.UTC()
    //new Date(ms_timestamp)
    //Date.now()
  //or "yyyy-mm-ddThh:nn:ssZ"
  let parts=new Intl.DateTimeFormat(
    "en",{
      timeZone:zoneName,hour12:false,
      year:"numeric",month:"2-digit,"day:"2-digit",
      hour:"2-digit",minute:"2-digit",second:"2-digit",
      fractionalSecondDigits:3
      }
    ).formatToParts(new Date(date));
  let y=parts.find(e=>e.type=="year").value;
  let m=parts.find(e=>e.type=="month").value;
  let d=parts.find(e=>e.type=="day").value;
  let h=parts.find(e=>e.type=="hour").value;
  let n=parts.find(e=>e.type=="minute").value;
  let s=parts.find(e=>e.type=="second").value;
  let f=parts.find(e=>e.type=="fractionalSecond").value;
  let date2=new Date(y+"-"+m+"-"+d+"T"+h+":"+n+":"+s+"."+f+"Z");
  return (date2.getTime()-date.getTime())/1000/60
  };

Eventually, you will have to put it in a try catch block.

added 9 characters in body
Source Link

Get time offset (minutes) of timezone name

function getOffset(zoneName) {
  let parts=new Intl.DateTimeFormat(
    "en",{
      timeZone:zoneName,hour12:false,day:"numeric",
      hour:"numeric",minute:"numeric"
      }
    ).formatToParts(new Date("2024-01-02T12:00Z"));
  //change the month (01) according to your DST needs
  let d=parts.find(e=>e.type=="day").value;
  let h=parts.find(e=>e.type=="hour").value;
  let n=parts.find(e=>e.type=="minute").value;
  return (n-0)+(h*60)+(d*60*24)-60*12-60*24*2;
  };

You can adjust the year and month (and partially the time as well) at will, but the day must be >1 and <the last day in the month so that the month stays the same after the time shift due to this kind of calculation.

Alternatively, you can reparse the shifted values as a new Date in UTC and get the difference. Then you can use an arbitrary date (and time):

function getOffset(date,zoneName) {
  //date as Date object 
    //Date.UTC()
    //new Date(ms_timestamp)
    //Date.now()
  //or "yyyy-mm-ddThh:nn:ssZ"
  let parts=new Intl.DateTimeFormat(
    "en",{
      timeZone:zoneName,hour12:false,
      year:"numeric",month:"2-digit,"day:"2-digit",
      hour:"2-digit",minute:"2-digit",second:"2-digit"
      }
    ).formatToParts(new Date(date));
  let y=parts.find(e=>e.type=="year").value;
  let m=parts.find(e=>e.type=="month").value;
  let d=parts.find(e=>e.type=="day").value;
  let h=parts.find(e=>e.type=="hour").value;
  let n=parts.find(e=>e.type=="minute").value;
  let s=parts.find(e=>e.type=="second").value
  let date2=new Date(y+"-"+m+"-"+d+"T"+h+":"+n+":"+s+"Z");
  return (date2.getTime()-date.getTime())/1000/60
  };

Eventually, you will have to put it in a try catch block.

Get time offset (minutes) of timezone name

function getOffset(zoneName) {
  let parts=new Intl.DateTimeFormat(
    "en",{
      timeZone:zoneName,hour12:false,day:"numeric",
      hour:"numeric",minute:"numeric"
      }
    ).formatToParts(new Date("2024-01-02T12:00Z"));
  //change the month (01) according to your DST needs
  let d=parts.find(e=>e.type=="day").value;
  let h=parts.find(e=>e.type=="hour").value;
  let n=parts.find(e=>e.type=="minute").value;
  return (n-0)+(h*60)+(d*60*24)-60*12-60*24*2;
  };

You can adjust the year and month (and partially the time as well) at will, but the day must be >1 and <the last day in the month so that the month stays the same after the time shift due to this kind of calculation.

Alternatively, you can reparse the shifted values as a new Date in UTC and get the difference. Then you can use an arbitrary date (and time):

function getOffset(date,zoneName) {
  //date as Date object 
  //Date.UTC()
  //new Date(ms_timestamp)
  //Date.now()
  //"yyyy-mm-ddThh:nn:ssZ"
  let parts=new Intl.DateTimeFormat(
    "en",{
      timeZone:zoneName,hour12:false,
      year:"numeric",month:"2-digit,"day:"2-digit",
      hour:"2-digit",minute:"2-digit",second:"2-digit"
      }
    ).formatToParts(new Date(date));
  let y=parts.find(e=>e.type=="year").value;
  let m=parts.find(e=>e.type=="month").value;
  let d=parts.find(e=>e.type=="day").value;
  let h=parts.find(e=>e.type=="hour").value;
  let n=parts.find(e=>e.type=="minute").value;
  let s=parts.find(e=>e.type=="second").value
  let date2=new Date(y+"-"+m+"-"+d+"T"+h+":"+n+":"+s+"Z");
  return (date2.getTime()-date.getTime())/1000/60
  };

Eventually, you will have to put it in a try catch block.

Get time offset (minutes) of timezone name

function getOffset(zoneName) {
  let parts=new Intl.DateTimeFormat(
    "en",{
      timeZone:zoneName,hour12:false,day:"numeric",
      hour:"numeric",minute:"numeric"
      }
    ).formatToParts(new Date("2024-01-02T12:00Z"));
  //change the month (01) according to your DST needs
  let d=parts.find(e=>e.type=="day").value;
  let h=parts.find(e=>e.type=="hour").value;
  let n=parts.find(e=>e.type=="minute").value;
  return (n-0)+(h*60)+(d*60*24)-60*12-60*24*2;
  };

You can adjust the year and month (and partially the time as well) at will, but the day must be >1 and <the last day in the month so that the month stays the same after the time shift due to this kind of calculation.

Alternatively, you can reparse the shifted values as a new Date in UTC and get the difference. Then you can use an arbitrary date (and time):

function getOffset(date,zoneName) {
  //date as Date object 
    //Date.UTC()
    //new Date(ms_timestamp)
    //Date.now()
  //or "yyyy-mm-ddThh:nn:ssZ"
  let parts=new Intl.DateTimeFormat(
    "en",{
      timeZone:zoneName,hour12:false,
      year:"numeric",month:"2-digit,"day:"2-digit",
      hour:"2-digit",minute:"2-digit",second:"2-digit"
      }
    ).formatToParts(new Date(date));
  let y=parts.find(e=>e.type=="year").value;
  let m=parts.find(e=>e.type=="month").value;
  let d=parts.find(e=>e.type=="day").value;
  let h=parts.find(e=>e.type=="hour").value;
  let n=parts.find(e=>e.type=="minute").value;
  let s=parts.find(e=>e.type=="second").value
  let date2=new Date(y+"-"+m+"-"+d+"T"+h+":"+n+":"+s+"Z");
  return (date2.getTime()-date.getTime())/1000/60
  };

Eventually, you will have to put it in a try catch block.

added 38 characters in body
Source Link

Get time offset (minutes) of timezone name

function getOffset(zoneName) {
  let parts=new Intl.DateTimeFormat(
    "en",{
      timeZone:zoneName,hour12:false,day:"numeric",
      hour:"numeric",minute:"numeric"
      }
    ).formatToParts(new Date("2024-01-02T12:00Z"));
  //change the month (01) according to your DST needs
  let d=parts.find(e=>e.type=="day").value;
  let h=parts.find(e=>e.type=="hour").value;
  let n=parts.find(e=>e.type=="minute").value;
  return (n-0)+(h*60)+(d*60*24)-60*12-60*24*2;
  };

You can adjust the year and month (and partially the time as well) at will, but the day must be >1 and <the last day in the month so that the month stays the same after the time shift due to this kind of calculation.

Alternatively, you can reparse the shifted values as a new Date in UTC and get the difference. Then you can use an arbitrary date (and time):

function getOffset(date,zoneName) {
  //date as UTC Date object 
  //Date.UTC() 
 or //new Date(ms_timestamp) 
 or //Date.now()
  //"yyyy-mm-ddThh:nn:ssZ"
  let parts=new Intl.DateTimeFormat(
    "en",{
      timeZone:zoneName,hour12:false,
      year:"numeric",month:"2-digit,"day:"2-digit",
      hour:"2-digit",minute:"2-digit",second:"2-digit"
      }
    ).formatToParts(new Date(date));
  let y=parts.find(e=>e.type=="year").value;
  let m=parts.find(e=>e.type=="month").value;
  let d=parts.find(e=>e.type=="day").value;
  let h=parts.find(e=>e.type=="hour").value;
  let n=parts.find(e=>e.type=="minute").value;
  let s=parts.find(e=>e.type=="second").value
  let date2=new Date(y+"-"+m+"-"+d+"T"+h+":"+n+":"+s+"Z");
  return (date2.getTime()-date.getTime())/1000/60
  };

Eventually, you will have to put it in a try catch block.

Get time offset (minutes) of timezone name

function getOffset(zoneName) {
  let parts=new Intl.DateTimeFormat(
    "en",{
      timeZone:zoneName,hour12:false,day:"numeric",
      hour:"numeric",minute:"numeric"
      }
    ).formatToParts(new Date("2024-01-02T12:00Z"));
  //change the month (01) according to your DST needs
  let d=parts.find(e=>e.type=="day").value;
  let h=parts.find(e=>e.type=="hour").value;
  let n=parts.find(e=>e.type=="minute").value;
  return (n-0)+(h*60)+(d*60*24)-60*12-60*24*2;
  };

You can adjust the year and month (and partially the time as well) at will, but the day must be >1 and <the last day in the month so that the month stays the same after the time shift due to this kind of calculation.

Alternatively, you can reparse the shifted values as a new Date in UTC and get the difference. Then you can use an arbitrary date (and time):

function getOffset(date,zoneName) {
  //date as UTC Date object Date.UTC() or new Date(ms_timestamp) or "yyyy-mm-ddThh:nn:ssZ"
  let parts=new Intl.DateTimeFormat(
    "en",{
      timeZone:zoneName,hour12:false,
      year:"numeric",month:"2-digit,"day:"2-digit",
      hour:"2-digit",minute:"2-digit",second:"2-digit"
      }
    ).formatToParts(new Date(date));
  let y=parts.find(e=>e.type=="year").value;
  let m=parts.find(e=>e.type=="month").value;
  let d=parts.find(e=>e.type=="day").value;
  let h=parts.find(e=>e.type=="hour").value;
  let n=parts.find(e=>e.type=="minute").value;
  let s=parts.find(e=>e.type=="second").value
  let date2=new Date(y+"-"+m+"-"+d+"T"+h+":"+n+":"+s+"Z");
  return (date2.getTime()-date.getTime())/1000/60
  };

Eventually, you will have to put it in a try catch block.

Get time offset (minutes) of timezone name

function getOffset(zoneName) {
  let parts=new Intl.DateTimeFormat(
    "en",{
      timeZone:zoneName,hour12:false,day:"numeric",
      hour:"numeric",minute:"numeric"
      }
    ).formatToParts(new Date("2024-01-02T12:00Z"));
  //change the month (01) according to your DST needs
  let d=parts.find(e=>e.type=="day").value;
  let h=parts.find(e=>e.type=="hour").value;
  let n=parts.find(e=>e.type=="minute").value;
  return (n-0)+(h*60)+(d*60*24)-60*12-60*24*2;
  };

You can adjust the year and month (and partially the time as well) at will, but the day must be >1 and <the last day in the month so that the month stays the same after the time shift due to this kind of calculation.

Alternatively, you can reparse the shifted values as a new Date in UTC and get the difference. Then you can use an arbitrary date (and time):

function getOffset(date,zoneName) {
  //date as Date object 
  //Date.UTC() 
  //new Date(ms_timestamp) 
  //Date.now()
  //"yyyy-mm-ddThh:nn:ssZ"
  let parts=new Intl.DateTimeFormat(
    "en",{
      timeZone:zoneName,hour12:false,
      year:"numeric",month:"2-digit,"day:"2-digit",
      hour:"2-digit",minute:"2-digit",second:"2-digit"
      }
    ).formatToParts(new Date(date));
  let y=parts.find(e=>e.type=="year").value;
  let m=parts.find(e=>e.type=="month").value;
  let d=parts.find(e=>e.type=="day").value;
  let h=parts.find(e=>e.type=="hour").value;
  let n=parts.find(e=>e.type=="minute").value;
  let s=parts.find(e=>e.type=="second").value
  let date2=new Date(y+"-"+m+"-"+d+"T"+h+":"+n+":"+s+"Z");
  return (date2.getTime()-date.getTime())/1000/60
  };

Eventually, you will have to put it in a try catch block.

added 26 characters in body
Source Link
Loading
added 15 characters in body
Source Link
Loading
added 10 characters in body
Source Link
Loading
added 8 characters in body
Source Link
Loading
deleted 9 characters in body
Source Link
Loading
Improved ideas
Source Link
Loading
added 44 characters in body
Source Link
Loading
added 1136 characters in body
Source Link
Loading
added 59 characters in body
Source Link
Loading
deleted 5 characters in body
Source Link
Loading
Source Link
Loading