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

  • SEARCH
  • Home
  • 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 401543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:02:37+00:00 2026-05-12T17:02:37+00:00

can anybody spot any mistake in this function? .. This is a function which

  • 0

can anybody spot any mistake in this function? .. This is a function which receives a string of type yyyy-mm-dd hh:mm:ss aa and converts to UTC and builds up a string yyyy-mm-dd hh:mm:ss.

function LocalTimetoUTC(localTime)
{

    var time = localTime.split(" "); //Received :- yyyy-mm-dd hh:mm:ss aa
    var yearday = time[0].split("-");
    var dateTime = time[1].split(":");
    var ampm = time[2];
    var hours = 0;

    var year = yearday[0];
    var month = yearday[1]-1; 
    var day = yearday[2];

    hours = dateTime[0];
    var minutes  = dateTime[1];
    var seconds = dateTime[2];

    /* We have to first convert it to 24 hour format
     * 12:00:00 AM : 00:00:00
     * 12:00:00 PM : 12:00:00 
     * Anytime other than 12
     * 1:00:00 AM   : 1:00:00
     * 1:00:00 PM   : 13:00:00
     */
    if(ampm == "PM")
    {       
        //If it is 12PM, adding 12 will create a problem
        if(hours != 12)
        {
            hours +=12;
        }
    }
    else //AM CASE 
    {   
        if(hours == 12)
        {
            hours = 00;
        }
    }


    var now = new Date(year,month,day,hours,minutes,seconds);

    var utcString = now.getUTCFullYear()+"-"
                  +(now.getUTCMonth()+1)+"-"+now.getUTCDate()+""
                   +now.getUTCHours()+":"+now.getUTCMinutes()+":"+now.getUTCSeconds();

    return utcString;
}
  • 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-12T17:02:37+00:00Added an answer on May 12, 2026 at 5:02 pm

    Your primary problem is you are using strings in numeric operations. In addition your output formatting has some problems too. Here is an untested refactoring:-

    function convertToUTCString(localTime)
    {
      var dateTimeComponents = localTime.split(" ");
      var dateComponent = dateTimeComponents[0].split("-");
      var timeComponent = dateTimeComponents[1].split(":");
      var ampm = dateTimeComponents[2]
    
      var dat = new Date(parseInt(dateComponent[0]),
        parseInt(dateComponent[1]) -1,
        parseInt(dateComponent[2]),
        parseInt(timeComponent[0]) % 12,
        parseInt(timeComponent[1]),
        parseInt(timeComponent[2]) );
    
      if (ampm == "PM")  // do all locales use 'AM' / 'PM' ??
      {
        dat.setHours(dat.getHours() + 12);
      }
    
      function pad(val)
      {
        var s = val.toString();
        return s.length < 2 ? "0" + s : s
      }
    
      return dat.getUTCFullYear() + "-" +
        pad((dat.getUTCMonth() + 1 )) + "-" + 
        pad(dat.getUTCDate()) + " " + 
        pad(dat.getUTCHours()) + ":" + 
        pad(dat.getUTCMinutes()) + ":" +
        pad(dat.getUTCSeconds());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 509k
  • Answers 509k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You very well may have, but did you try using… May 16, 2026 at 4:33 pm
  • Editorial Team
    Editorial Team added an answer I would use something along these lines... Just coded this… May 16, 2026 at 4:33 pm
  • Editorial Team
    Editorial Team added an answer I've found that it's all explained here: http://developer.apple.com/iphone/library/featuredarticles/FA_Wireless_Enterprise_App_Distribution/Introduction/Introduction.html The developer… May 16, 2026 at 4:33 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Can anybody please spot my error, this should be a legal query in SQL
Can anybody help me with this: I want select all records from datatable which
Can anybody provide a brief explanation of the differences between mongrel and mongrel cluster?
Can anybody explain why I see another web site at my http secure address.
Can anybody tell what is the use of a BroadcastReceiver and give an example
can anybody give example with service in android? Thanks
Can anybody provide me some sample example on Client and server connection using sockets
Possible Duplicate: Can anybody suggest the best image resize script in php? I'm still
int main() { double i=4; printf(%d,i); return 0; } Can anybody tell me why
In a user control, I've got a Repeater inside of an UpdatePanel (which id

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.