skip to Main Content

While fetching data from a weather API the result time zone for Indian location was 19800, which was shift in seconds from UTC to real clock time.

timezone : 19800

I need that to real clock time. ChatGPT gave me this:

var shiftInSeconds = 19800;

var currentTimeUTC = Date.now();
var shiftedTime = currentTimeUTC + (shiftInSeconds * 1000); // Convert seconds to milliseconds
var shiftedDate = new Date(shiftedTime);
var localTime = shiftedDate.toLocaleString();

console.log("Real time:", localTime);

I got the result as

Real time: 17/05/2024, 21:27:43

The actual time now is 03:57 PM. Is there any way to get the exact local clock time ?

2

Answers


  1. Does this work for you ?

    I switched to the toISOString() function instead since you’re actually using UTC time

    The internal time representation is accurate already. It’s just the way you printed it.

    
    var shiftInSeconds = 19800;
    
    var currentTimeUTC = Date.now();
    var shiftedTime = currentTimeUTC + (shiftInSeconds * 1000); // Convert seconds to milliseconds
    var shiftedDate = new Date(shiftedTime);
    var localTime = shiftedDate.toISOString();
    
    console.log("Real time:", localTime);
    
    Login or Signup to reply.
  2. maybe like this ?

    const newDate = new Date();
    const myDate = newDate.toLocaleTimeString("en-IN");
    console.log(timedDate);
    

    output : "12:46:20 pm" witch is the real time for me.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search