2

I am using the HTML5 element datetime-local. I need to have two formats of the date. One as a date object the other as a string. I am going to store the date object in the database and I am going to use the string to set the datetime-local form input.

I need to convert this string to a date object:
"2014-06-22T16:01"

I can't seem to get the correct time. This is what I am getting. The time not correct.
Sun Jun 22 2014 09:01:00 GMT-0700 (PDT)

This is the how I am formating the date:

function formatTime(_date) {
var _this = this,
    date    = (_date) ? _date : new Date(),
    day     = date.getDate(),
    month   = date.getMonth() + 1,
    year    = date.getFullYear(),
    hour    = date.getHours(),
    minute  = date.getMinutes(),
    seconds = date.getSeconds(),

function addZero(num) {
  return num > 9 ? num : '0' + num;
}

minute  = addZero(minute);
seconds = addZero(seconds);
hour    = addZero(hour);

day     = addZero(day);
month   = addZero(month);

return year + '-' + month + '-' + day + 'T' + hour + ':' + minute;

};

Example: http://codepen.io/zerostyle/pen/gwpuK/

zero
  • 989
  • 11
  • 18
  • for UTC, ISO issues, I think you have already got an answer. For Date Formatting, please check this link - http://stackoverflow.com/questions/4744299/how-to-get-datetime-in-javascript/24328303#24328303 – Harpreet Singh Jun 23 '14 at 01:33

1 Answers1

3

If you are trying to get an ISO 8601 date string, you can try Date.prototype.toISOString. However, it always uses UTC. If you want to include the local timezone, use something like the following:

/* Return a string in ISO 8601 format with current timezone offset
** e.g. 2014-10-02T23:31:03+0800
** d is a Date object, or defaults to current Date if not supplied.
*/
function toLocalISOString(d) {

   // Default to now if no date provided
   d = d || new Date();

  // Pad to two digits with leading zeros
  function pad(n){
    return (n<10?'0':'') + n;
  }

  // Pad to three digits with leading zeros
  function padd(n){
    return (n<100? '0' : '') + pad(n);
  }

  // Convert offset in mintues to +/-HHMM
  // Note change of sign
  // e.g. -600 => +1000, +330 => -0530
  function minsToHHMM(n){
    var sign = n<0? '-' : '+';
    n = Math.abs(n);
    var hh = pad(n/60 |0);
    var mm = pad(n%60);
    return  sign + hh + mm;
  }

  var offset = minsToHHMM(d.getTimezoneOffset() * -1);

  return d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate()) +
         'T' + pad(d.getHours()) + ':' + pad(d.getMinutes()) + ':' + pad(d.getSeconds()) +
         '.' + padd(d.getMilliseconds()) + offset;
}

console.log(toLocalISOString(new Date())); // 2014-06-23T07:58:04.773+0800 

Edit

The above probably misses your question, which seems to be;

I need to convert this string to a date object: "2014-06-22T16:01"

Presumaly you want to treat it as a local time string. ECMA-262 says that ISO–like strings without a timezone are to be treated as UTC, and that is what your host seems to be doing. So you need a function to create a local Date object from the string:

function parseYMDHM(s) {
  var b = s.split(/\D+/);
  return new Date(b[0], --b[1], b[2], b[3], b[4], b[5]||0, b[6]||0);
}

console.log(parseYMDHM('2014-06-22T16:01')); // Sun Jun 22 16:01:00 UTC+0800 2014
RobG
  • 142,382
  • 31
  • 172
  • 209
  • I might not have communicated my problem right. The main problem is the time that get's set is 5 hours off. The time is getting converted to the future. What am I missing? – zero Jun 23 '14 at 15:05