Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Get Time Central Time Zone

Tags:

javascript

I am using a button to update a form field with a date and timestamp. The issue now is that the request has been made so that anytime these are used they are being updated to the central time zone. Can anyone help me update the below so I can accomodate?

function getTimeStamp() {
  var now = new Date();
  return ((now.getMonth() + 1) + '/' + (now.getDate()) + '/' + now.getFullYear() + " " + now.getHours() + ':' +
    ((now.getMinutes() < 10) ? ("0" + now.getMinutes()) : (now.getMinutes())) + ':' + ((now.getSeconds() < 10) ? ("0" + now
      .getSeconds()) : (now.getSeconds())));
}

function setTime() {
  document.getElementById('InsertRecordDate_Received').value = getTimeStamp();
}
like image 301
user2653658 Avatar asked Oct 20 '25 17:10

user2653658


2 Answers

From http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/6016329

/** 
 * function to calculate local time
 * in a different city
 * given the city's UTC offset
 */
function calcTime(city, offset) {

    // create Date object for current location
    var d = new Date();

    // convert to msec
    // add local time zone offset
    // get UTC time in msec
    var utc = d.getTime() + (d.getTimezoneOffset() * 60000);

    // create new Date object for different city
    // using supplied offset
    var nd = new Date(utc + (3600000*offset));

    // return time as a string
    return "The local time in " + city + " is " + nd.toLocaleString();
}
like image 183
jdmdevdotnet Avatar answered Oct 22 '25 05:10

jdmdevdotnet


Check out moment.js, and its complement moment-timezone.js:

http://momentjs.com

http://momentjs.com/timezone

For example, this will output the current time converted to central timezone:

moment().tz('America/Chicago').format('hh:mm:ss z')
> 03:48:34 CST

moment().tz('America/Chicago').format('hh:mm:ss z Z')
> 03:50:35 CST -06:00

moment().tz('America/Chicago').format()
> 2016-12-05T15:52:09-06:00
like image 23
Hristo Avatar answered Oct 22 '25 07:10

Hristo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!