1

I need to create a date in this format:

2016-02-18T13:24:10-08:00

year-month-dayThour:minutes:secondsTIMEZONE

I try this:

var currentTime = new Date();

var year = currentTime.getFullYear();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var hour = currentTime.getHours();
var min = currentTime.getMinutes();
var secs = currentTime.getSeconds();


var time = year + "-" + month + "-" + day + "T" + hour + ":" + min + ":" + secs;

alert(time);

but I have two problems:

1) I need all values to has a zero if they have only one digit.

for example, today prints 2016-2-18. I need this 2 to be 02.

2) how can I get the timezone like in the example?

-08:00
+03:00
...

https://jsfiddle.net/1vxpobbw/

thank you friends!

RGS
  • 4,062
  • 4
  • 31
  • 67
  • What are you using as the base timezone reference to +- from? UTC? GMT? – mhodges Feb 18 '16 at 23:33
  • 3
    There are libraries such as [momentjs](http://momentjs.com/) that will do this for you – Alon Eitan Feb 18 '16 at 23:37
  • @AlonEitan oh, but is that hard to get user timezone in jquery? – RGS Feb 18 '16 at 23:40
  • 2
    Moment is so popular I wouldn't even think somebody would spend time messing with dates and times manually. Seriously, it's a well-developed, properly maintained open source Javascript library. I'd strongly recommend using it and not inventing a wheel. – rishat Feb 18 '16 at 23:40

3 Answers3

2

1)

you just need to pad values with the desired number of zeroes

function ZeroPad(str, n)
{
    while( str.length < n )
    {
        str = '0' + str;
    }
    return str;
}

as you have month = 2

just call

ZeroPad( month, 2 )

you'll get

02

2)

To get the timezone offset (if I understood the question) just look here

Getting the client's timezone in JavaScript

As you have the offset proceed like you did with hour-date.

Community
  • 1
  • 1
Paolo
  • 15,233
  • 27
  • 70
  • 91
2

Maybe you should code like this?

var currentTime = new Date();

var year = currentTime.getFullYear();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var hour = currentTime.getHours();
var min = currentTime.getMinutes();
var secs = currentTime.getSeconds();

// Checking if the numbers are lower from 10, then add 0 before of it
if(year < 10) year = "0" + year
if(month < 10) month = "0" + month
if(day < 10) day = "0" + day
if(min < 10) min = "0" + min
if(hour < 10) hour = "0" + hour
if(secs < 10) secs = "0" + secs

var time = year + "-" + month + "-" + day + "T" + hour + ":" + min + ":" + secs;

alert(time);

It is checking if the hour, min or secs are lower than 10. If yes, it is adding "0" before of it.

mr_echo
  • 86
  • 4
1

Okay, this is the full answer including zero padding the dates as well as adding the timezone difference from GMT.

$( document ).ready(function() {
  var currentTime = new Date();

  var year = currentTime.getFullYear();
  var month = currentTime.getMonth() + 1;
  var day = currentTime.getDate();
  var hour = currentTime.getHours();
  var min = currentTime.getMinutes();
  var secs = currentTime.getSeconds();
  var timezoneOffset = currentTime.getTimezoneOffset();
  var offsetHours = Math.floor(timezoneOffset / 60);
  var offsetMinutes = timezoneOffset % 60;

  if(month < 10) month = "0" + month;
  if(day < 10) day = "0" + day;
  if(min < 10) min = "0" + min;
  if(hour < 10) hour = "0" + hour;
  if(secs < 10) secs = "0" + secs;
  if(offsetHours < 10) offsetHours = "0" + offsetHours;
  if(offsetMinutes < 10) offsetMinutes = "0" + offsetMinutes;

  var time = year + "-" + month + "-" + day + "T" + hour + ":" + min + ":" + secs + (offsetHours > 0 ? "+": "-") + offsetHours + ":" + offsetMinutes;

  alert(time);

});
mhodges
  • 10,938
  • 2
  • 28
  • 46