0

I need to get the end of the month in epoch time according to each user's timezone to provide user-friendly experience.

As an example for a user in Sri Lanka, it could be March while in the US it's still February. I need a reliable way to calculate the timestamp without interference from the server locale time. So I could host it anywhere in the world without running into a problem

It should be calculated as follows,

  • get current year and month according to the locale timezone

I tried to use Intl API but couldn't figure out

  • then get the last day of this month or fisrt day of next month
new Date (locale_year,locale_month+1,0)
  • convert to timestamp
.valueOF()

tried to use the timezone option TimeZone in JS Intl API

Shankaja Aroshana
  • 551
  • 1
  • 3
  • 17

1 Answers1

0

After experimenting with all the Date.prototypes & Intl.DateTimeFormat

This function helps to make logic according to the each user's Timezone. Without the unforseen server localtime interference

Visit the IANA time zone database for more timezone identifiers, such as Asia/Shanghai, Asia/Kolkata, America/New_York.

function dateWithTZone (timezone,date){

    const options = {
      year: "numeric",
      month: "numeric",
      day: "numeric",
      hour: "numeric",
      minute: "numeric",
      second: "numeric",
      hour12: false,
      timeZone: timezone,
      timeZoneName: "longOffset",
    };
    const dateArray = new Intl.DateTimeFormat("en-US", options).formatToParts(date);
    const dateObj = dateArray.reduce((acc, cur) => ({...acc,[cur.type]: cur.value}),{});
    const {hourOffset,minuteOffset} = getOffset(dateObj.timeZoneName)
    return {...dateObj,hourOffset,minuteOffset}
}
function getOffset (timeZoneName){
    return {
      hourOffset: parseInt(
        timeZoneName.slice(3, 4) + timeZoneName.slice(4, 6)
      ),
      minuteOffset: parseInt(
        timeZoneName.slice(3, 4) + timeZoneName.slice(7, 9)
      ),
    };
}

month returned from dateWithTZone() has values 1-12. Normally Javascript month values are from 0-11.

// get the timezone from the user. here I use "Asia/Kolkata"
const {year,month,hourOffset,minuteOffset} = dateWithTZone("Asia/Kolkata",Date.now())
// I wanted to get user's current month end timestamp 
Date.UTC(year,month,1,-hourOffset,-minuteOffset)
  

Shankaja Aroshana
  • 551
  • 1
  • 3
  • 17
  • This doesn't do what the OP wants. It will get the offset for now, not for the required date and time. Offsets change due to daylight saving and historic changes, you can't assume it was or will always be whatever it is now. See [*How to initialize a JavaScript Date to a particular time zone*](https://stackoverflow.com/questions/15141762/how-to-initialize-a-javascript-date-to-a-particular-time-zone) and [*How do I specify the time zone when creating a JavaScript Date?*](https://stackoverflow.com/questions/20834411/how-do-i-specify-the-time-zone-when-creating-a-javascript-date) – RobG Mar 14 '23 at 22:58
  • If anyone needs the daylight saving and historical changes ,consider using [`getTimezoneOffset()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset#gettimezoneoffset_and_dst) .Thanks for the help [RobG](https://stackoverflow.com/users/257182/robg) – Shankaja Aroshana Mar 15 '23 at 07:49