Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6578647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:47:50+00:00 2026-05-25T15:47:50+00:00

I write a function to reverse UTC time to local time function utcToLocal(utc){ var

  • 0

I write a function to reverse UTC time to local time

function utcToLocal(utc){
    var t = new Date(Number(utc));
    d = [t.getFullYear(), t.getMonth(), t.getDate()].join('/');
    d += ' ' + t.toLocaleTimeString();
    return d;
}

but i can’t confirm this code is right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:47:51+00:00Added an answer on May 25, 2026 at 3:47 pm

    You should be able to convert the UTC timestamp into a local date and just subtract the local offset (which is in minutes), so:

    function utcToLocal(utc){
        // Create a local date from the UTC string
        var t = new Date(Number(utc));
    
        // Get the offset in ms
        var offset = t.getTimezoneOffset()*60000;
    
        // Subtract from the UTC time to get local
        t.setTime(t.getTime() - offset);
    
        // do whatever
        var d = [t.getFullYear(), t.getMonth(), t.getDate()].join('/');
        d += ' ' + t.toLocaleTimeString();
        return d;
    }
    

    Where I am, the offset is -600, so I need to subtract -36,000,000 ms from UTC time (which actually adds 36,000,000 ms).

    Edit

    I may have misunderstood the question.

    The internal value of a javascript date instance is a UTC time clip in milliseconds. So if utc is such a time (e.g. 2012-08-19T00:00:00Z is 1345334400000), then the OP will create a date instance based on that value and toLocaleTimeString will show an implementation dependent string of the local time for the supplied UTC time.

    So if the local timezone offset is say -6hrs, then alert(new Date(1345334400000))) show something like Sat Aug 18 2012 18:00:00 GMT-600.

    I was assuming that the OP wanted to set the local time to the same time as the UTC time, e.g. that 2012-08-19T00:00:00Z would become 2012-08-19T00:00:00 local.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just had to write a string reverse function in C# 2.0 (i.e. LINQ
In C/C++, there is a 'write() function which let me write to either file
You write a function and, looking at the resulting assembly, you see it can
I want to write a function in Python that returns different fixed values based
I want to write a function that takes an array of letters as an
I want to write a function that accepts two objects as parameters and compare
I want to write a function that returns the nearest next power of 2
I need to write a function that takes 4 bytes as input, performs a
I want to write a function that accepts a parameter which can be either
I wanted to write a function that would take an object and convert it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.