0

I am based in Australia and while new Date() give me the current date and time in Australia, for instance Fri Aug 26 2016 09:16:16 GMT+1000 (AUS Eastern Standard Time) , if I write new Date().toJSON() I get 2016-08-25T23:20:08.242Z, how can I get the same format as in yyyy-mm-ddThh:mn:ss but keeping my local day and time, ie it should be the 26 and not the 25th.

Edit: when I write programmatically new Date(2016, 11, x) with var x = 31, using toJSON() I have no guarantee to see displayed 2016-12-31 because of timezones, so was wondering is there is a different javascript function that would give me the intended result.

Bondifrench
  • 1,272
  • 1
  • 20
  • 35
  • `Date.prototype.toJSON = Date.prototype.toString` – Jaromanda X Aug 26 '16 at 02:12
  • The date isn't wrong, it's in UTC. Without timezone information, `yyyy-mm-ddThh:mn:ss` is meaningless. – Blender Aug 26 '16 at 02:13
  • Possible duplicate of [Where can I find documentation on formatting a date in JavaScript?](http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – Jasen Aug 26 '16 at 02:19
  • Maybe the wording is not clear enough, but my objective is to keep the local day and time, if I do `new Date(2016, 11, 31).toJSON()`depending on my local time, I get `2016-12-30` which is not my intention. – Bondifrench Aug 26 '16 at 02:19

3 Answers3

1

I would use moment.js for that.

var date = moment("Fri Aug 26 2016 09:16:16 GMT+1000");
console.log(moment(date).format('YYYY-MM-DD T hh:mm:ss'));

https://jsfiddle.net/Refatrafi/ys4nu8o9/

Rafi Ud Daula Refat
  • 2,187
  • 19
  • 28
0

toJSON() returns timestamps in ISO 8601 format. Z at the end of string means that used UTC. Date objects in ECMAScript are internally UTC. The specification for Date.prototype.toJSON says that it uses Date.prototype.toISOString, which states that "the timezone is always UTC".

Sergey Gornostaev
  • 7,596
  • 3
  • 27
  • 39
  • While it does make me understand the difference, it doesn't tell me how to realize the conversion from one to another. – Bondifrench Aug 26 '16 at 02:07
0

The date isn't wrong, it's in UTC. Without timezone information, yyyy-mm-ddThh:mn:ss is meaningless unless you explicitly want to assume that it's in the AEST timezone.

If you're transmitting the date as a string to be parsed back into some sort of Date-like object later on (by your webserver, for example), there's nothing you need to do. 2016-08-25T23:20:08.242Z unambiguously refers to the same point in time no matter what you use to parse it.

If you're trying to format the date object and display it somewhere, you can extract the different parts of the Date object and build up the representation you want:

function format_date(d) {
    var pretty_date = [d.getFullYear(), d.getMonth() + 1, d.getDate()].join('-');
    var pretty_time = [d.getHours(), d.getMinutes(), d.getSeconds()].join(':');

    return pretty_date + 'T' + pretty_time;
}

As the other answers have pointed out, if you plan on working more with dates, consider using a library that makes it easier. JavaScript doesn't have a very rich API, so you'll have to write more code.

Blender
  • 289,723
  • 53
  • 439
  • 496
  • At the end of day what I am aiming for is, when I write programatically `new Date(2016, 11, 31)`, with years, months and date being variables and then do a transformation to it, I get in return `2016-12-31`, which `toJSON()` almost does, but depending on the time of the day, sometimes I get `2016-12-30` which is not what I want displayed. – Bondifrench Aug 26 '16 at 02:42
  • @Bondifrench: `toJSON()` isn't going to do what you want. You need to write your own function to produce the formatted date. – Blender Aug 26 '16 at 02:44