By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – How to get the date for a specific time zone [closed]
JavaScript

Javascript – How to get the date for a specific time zone [closed]

Admin
Last updated: 2023/12/13 at 5:54 PM
Admin
Share
2 Min Read
How to get the date for a specific time zone [closed]

Problem:

Closed. This question needs details or clarity. It is not currently accepting answers.


Want to improve this question? Add details and clarify the problem by editing this post.

Contents
Problem:Solution:

Closed last month.

The community reviewed whether to reopen this question last month and left it closed:

Original close reason(s) were not resolved


Improve this question

I need to get the date for a time zone with javascript but Date instances get the date of the time zone in which the user is located.

This is the code I used:

const date = new Date();
let year = date.getFullYear();
let month = date.getMonth();
let day = date.getDate();

Note: I need an answer without the use of jquery.

Solution:

You can specify a time zone when using toLocaleString, toLocaleDateString, or toLocaleTimeString.

For example:

const now = new Date();
const s = now.toLocaleDateString(undefined, { timeZone: 'America/New_York' });
console.log(s);

Expand snippet

The undefined in the above code indicates to return the resulting string in a format appropriate for the user’s current language.

You can also specify a time zone when using Intl.DateTimeFormat directly, which offers further capabilities such as getting each part separately with formatToParts. For example:

function getDateValues(date, timeZone) {
    const fmt = new Intl.DateTimeFormat('en', { timeZone });
    const parts = fmt.formatToParts(date);

    let year, month, day;
    for (const part of parts) {
        switch (part.type) {
            case 'year':
                year = parseInt(part.value);
                break;
            case 'month':
                // note, months are 1-12 (not 0-11 like Date.getMonth)
                month = parseInt(part.value);
                break;
            case 'day':
                day = parseInt(part.value);
                break;
        }
    }

    return {year, month, day};
}

const now = new Date();
const values = getDateValues(new Date(), 'America/New_York');
console.log(values);

Expand snippet

Note in this case, I specify English ('en') for the language, so that I can be assured the resulting strings contain parsable numbers.

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article App component keeps calling the axios.get many times over in a react function Javascript – App component keeps calling the axios.get many times over in a react function
Next Article Make img fill entire height [closed] Javascript – Make img fill entire height [closed]
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?