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.

4
  • Date.prototype.toJSON = Date.prototype.toString Commented Aug 26, 2016 at 2:12
  • The date isn't wrong, it's in UTC. Without timezone information, yyyy-mm-ddThh:mn:ss is meaningless. Commented Aug 26, 2016 at 2:13
  • Possible duplicate of Where can I find documentation on formatting a date in JavaScript? Commented Aug 26, 2016 at 2: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. Commented Aug 26, 2016 at 2:19

3 Answers 3

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/

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks but adding an entire library for that seems a bit over the top.
@Bondifrench "over the top" comparing to what? Do you have an easy alternative solution?
@Bondifrench for dealing with date in js momentjs is awesome. what is wrong with that.
@Bondifrench have you found any easy alternative solution?
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".

1 Comment

While it does make me understand the difference, it doesn't tell me how to realize the conversion from one to another.
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.

2 Comments

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: toJSON() isn't going to do what you want. You need to write your own function to produce the formatted date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.