0

This is the code I currently have but I am struggling to convert it to UTC

var today = Date.UTC(new Date());
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
var H = today.getHours();
var i = today.getMinutes();
var s = today.getSeconds();

if(dd<10) {
    dd = '0'+dd
} 

if(mm<10) {
    mm = '0'+mm
} 

today = yyyy + '-' + mm + '-' + dd + ' ' + H + ':' + i + ':' + s;

Any ideas on how I can get this working in the same timestamp format? Thanks!

  • 4
    Possible duplicate of [How do you create a JavaScript Date object with a set timezone without using a string representation](https://stackoverflow.com/questions/439630/how-do-you-create-a-javascript-date-object-with-a-set-timezone-without-using-a-s) – Heretic Monkey Jul 06 '18 at 14:22
  • Possible duplicate of [How do you convert a JavaScript date to UTC?](https://stackoverflow.com/questions/948532/how-do-you-convert-a-javascript-date-to-utc) – Zenoo Jul 06 '18 at 14:22
  • I am trying that @HereticMonkey but i am having a hard time applying it to my code – Jake Stainer Jul 06 '18 at 14:27
  • Note the difference between the answer on that question, and what you have: The answer: `new Date(Date.UTC())`; your question: `new Date.UTC(new Date())`. – Heretic Monkey Jul 06 '18 at 14:29
  • I tried that but then I get `Uncaught TypeError: today.getDate is not a function` @HereticMonkey – Jake Stainer Jul 06 '18 at 14:31
  • times are very difficult. That's why i highly recommend libs like moment (big) or date-fns (small). – Thomas Jul 06 '18 at 14:34
  • @HereticMonkey he's not trying to create a date object with a different timezone, just extract the current date and time in UTC. – Alnitak Jul 06 '18 at 14:51
  • @Alnitak Then it's a duplicate of another question (possibly the one Zenoo pointed to). Feel free to find that and close it as a duplicate of that one. Answering just causes more clutter. – Heretic Monkey Jul 06 '18 at 14:55
  • @HereticMonkey none of the higher rated answers to that other question provide the answer in exactly the format requested, nor explain why this particular OP's approach is incorrect. – Alnitak Jul 06 '18 at 15:21
  • @Alnitak Obviously. Duplicates are not meant for exact matches to every single detail. It is expected that "professional and enthusiast programmers" can make the logical deductions necessary to adapt the answer to their needs. – Heretic Monkey Jul 06 '18 at 15:27

1 Answers1

1

Date objects are always stored in UTC - the .getXxx() functions you're calling implicitly convert that UTC time into your local timezone.

To extract the relevant fields in UTC time instead you should be using the .getUTCxxx() family of functions.

//
// returns the date and time in format 'YYYY-MM-DD hh:mm:ss'
//
// will take a `Date` object, or use the current system
// time if none is supplied
//
function UTC_DateTime(date) {
    if (date === undefined) {
         date = new Date();
    }

    function pad2(n) {
        return (n < 10) ? ('0' + n) : n;
    }

    return date.getUTCFullYear() + '-' +
           pad2(date.getUTCMonth() + 1) + '-' +
           pad2(date.getUTCDay()) + ' ' +
           pad2(date.getUTCHours()) + ':' +
           pad2(date.getUTCMinutes()) + ':' +
           pad2(date.getUTCSeconds());
}
Alnitak
  • 334,560
  • 70
  • 407
  • 495