0

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 ?

5
  • 2
    The script starts from Date.now() which already is your real time. I don't understand the problem. Please examples of dates you get from the API, and what you want as output for them.
    – trincot
    Commented May 17 at 10:52
  • Try shiftedDate.toUTCString(); and shiftedDate.toString(); so you can see how the output of the date object is adjusted by your local settings. Also output currentTimeUTC.toUTCString() / currentTimeUTC.toLocaleString() to you can see what you're starting with (and how the conversion from the date object is affected).
    – fdomn-m
    Commented May 17 at 11:26
  • @Tony - Your question is somewhat unclear. There's no need to apply a shift to get a "real time". new Date().toString() will always give the current local time - already "shifted" to the local time zone. Perhaps your weather API is simply telling you what time zone offset is applicable for the time and location of the weather report? It's hard to say without knowing which specific weather API you are using. But even if that's the case, you would still not necessarily need to apply it to the current time, nor shift it yourself manually. Commented May 17 at 19:09
  • See also stackoverflow.com/questions/15141762/… and my answer there, which may be helpful for your understanding of the Date object's behavior. Commented May 17 at 19:09
  • # Current Long (live) time format: Detailed stackoverflow.com/a/65871072 Commented May 30 at 17:56

3 Answers 3

1

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.

5
  • console.log with myDate, works Still, I need the 19800 sec away from UTC to be converted into real-time.
    – Tony
    Commented May 17 at 17:20
  • @Tony - no, you don't. That is already accounted for by the Date object, when it converts to local time. Commented May 17 at 19:01
  • @MattJohnson-Pint I can't use a new Date. I need the API provided time. That is 19800 or any other location
    – Tony
    Commented May 18 at 9:32
  • @Tony - 19800 is the offset from UTC (19800 / 3600 = 5.5 which means UTC+05:30). Where in your question is the timestamp returned by the API? I bet it's in UTC already. So then - do you want to display the corresponding local time at the weather location regardless of the user's own time zone? Or are you wanting to display the time in the user's own time zone? Commented May 20 at 19:47
  • want to display the corresponding local time at the weather location regardless of the user's own time zone. The API provides time as an offset from UTC.
    – Tony
    Commented May 21 at 4:53
1

This answer is based on your comment:

want to display the corresponding local time at the weather location regardless of the user's own time zone. The API provides time as an offset from UTC

The offset from UTC is not the time, it's the bias between UTC and the local time of the location - the time will be in a separate field in the response, or was provided in your request, and is usually in terms of UTC.

You do not need to shift the time by the offset with math. Doing so can lead to many other errors. You can instead provide the original UTC-based timestamp (as presumably returned by your weather API), and format it to a string in a given fixed offset using the timeZone option of toLocaleString (and similar Intl functions).

You do however, need to convert the offset from a number of seconds to an ISO 8601 compliant time zone offset string.

// Formats a Unix timestamp (in terms of milliseconds) to a string,
// to a fixed time zone offset (in terms of seconds).
function formatTimestamp(ts, offsetSeconds) {
    const lang = undefined; // use the current language
    const timeZone = formatOffset(offsetSeconds);
    return new Date(ts).toLocaleString(lang, { timeZone, timeZoneName: 'long' }); 
}

// Converts offset in seconds to string in ISO 8601 extended format.
// Example: 19800 => "+05:30"
function formatOffset(offsetSeconds) {
    const sign = offsetSeconds < 0 ? '-' : '+';
    const seconds = Math.abs(offsetSeconds);
    const hours = Math.floor(seconds / 3600);
    const minutes = Math.floor((seconds % 3600) / 60);
    return sign + ('0' + hours).slice(-2) + ':' + ('0' + minutes).slice(-2);
}

// The current time at UTC+05:30
console.log(formatTimestamp(Date.now(), 19800));

// A specific time at UTC-08:00
console.log(formatTimestamp(1704067200000, -28800));

I included timeZoneName: 'long' only for demonstration purposes. You can control the format of the output string using any of the options covered in the docs.

3
  • This code won't work in code snippet!
    – Tony
    Commented May 22 at 7:17
  • Works for me. It should work in any modern browser (Chrome, etc.) Commented May 22 at 16:12
  • I think the answer from GuessWhatBBQ is easy for me
    – Tony
    Commented May 22 at 16:39
0

Does this work for you ?

I'm forcing the object to print the UTC time (even though in your case the time zone was manually shifted to Indian Standard 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.toLocaleString("en-US", { timeZone: "UTC"});

console.log("Real time:", localTime);
6
  • This almost works. Is there any way to convert the time into a 12-hour format ?
    – Tony
    Commented May 17 at 17:24
  • I think you should stick to using toLocaleString and force the object to print the UTC time then (even though in your case the time zone was manually shifted to Indian Standard Time). Edited the code with changes. Commented May 17 at 18:05
  • Sorry, but this answer is incorrect. Please see "common errors" at stackoverflow.com/a/15171030/634824 Commented May 17 at 19:03
  • Yes I'm aware and I actually consulted your answer before writing mine, brilliant answer btw. I still opted for this because the api itself seems to be designed to be used with epoch shifting and I dont see a simpler way to make it work Commented May 17 at 19:14
  • @GuessWhatBBQ, Okay Now that ttoLocaleString("en-US", { timeZone: "UTC"}) works. Thanks!
    – Tony
    Commented May 18 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.