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 7650135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:10:28+00:00 2026-05-31T11:10:28+00:00

While writing a web application, it makes sense to store (server side) all datetimes

  • 0

While writing a web application, it makes sense to store (server side) all datetimes in the DB as UTC timestamps.

I was astonished when I noticed that you couldn’t natively do much in terms of Timezone manipulation in JavaScript.

I extended the Date object a little. Does this function make sense? Basically, every time I send anything to the server, it’s going to be a timestamp formatted with this function…

Can you see any major problems here? Or maybe a solution from a different angle?

Date.prototype.getUTCTime = function(){ 
  return new Date(
    this.getUTCFullYear(),
    this.getUTCMonth(),
    this.getUTCDate(),
    this.getUTCHours(),
    this.getUTCMinutes(), 
    this.getUTCSeconds()
  ).getTime(); 
}

It just seems a little convoluted to me. And I am not so sure about performance either.

  • 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-31T11:10:29+00:00Added an answer on May 31, 2026 at 11:10 am
    1. Dates constructed that way use the local timezone, making the constructed date incorrect. To set the timezone of a certain date object is to construct it from a date string that includes the timezone. (I had problems getting that to work in an older Android browser.)

    2. Note that getTime() returns milliseconds, not plain seconds.

    For a UTC/Unix timestamp, the following should suffice:

    Math.floor((new Date()).getTime() / 1000)
    

    It will factor the current timezone offset into the result. For a string representation, David Ellis’ answer works.

    To clarify:

    new Date(Y, M, D, h, m, s)
    

    That input is treated as local time. If UTC time is passed in, the results will differ. Observe (I’m in GMT +02:00 right now, and it’s 07:50):

    > var d1 = new Date();
    > d1.toUTCString();
    "Sun, 18 Mar 2012 05:50:34 GMT" // two hours less than my local time
    > Math.floor(d1.getTime()/ 1000)
    1332049834 
    
    > var d2 = new Date( d1.getUTCFullYear(), d1.getUTCMonth(), d1.getUTCDate(), d1.getUTCHours(), d1.getUTCMinutes(), d1.getUTCSeconds() );
    > d2.toUTCString();
    "Sun, 18 Mar 2012 03:50:34 GMT" // four hours less than my local time, and two hours less than the original time - because my GMT+2 input was interpreted as GMT+0!
    > Math.floor(d2.getTime()/ 1000)
    1332042634
    

    Also note that getUTCDate() cannot be substituted for getUTCDay(). This is because getUTCDate() returns the day of the month; whereas, getUTCDay() returns the day of the week.

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

Sidebar

Related Questions

I'm writing web application where I need to push data from server to the
While writing a login system for a web project Im working on I came
I'm writing a web site (C#, ASP 3.5) while implementing a simple CMS. In
While writing a .jsp file, can i include all the html5 tags in it
I'm writing an aggregation application which scrapes data from a couple of web sources
I'm writing a web application for public consumption...How do you get over/ deal with
I'm writing a web application using a <canvas> element. For simplicity, I have stripped
I'm writing a web application in Symfony for the first time, so I have
I am writing a web application with Linked In OAuth authentication. I use simple-linkedinphp
I'm writing a django web application, and I'm making unittests & selenium functional tests.

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.