Title: Return date and time in ("YYYY-MM-DD HH:mm") format when using moment.js (timezone)
Tags: javascript;angularjs;datetime;timezone;momentjs
Question: I am using moment-timezone so I can convert from selected timezone to timezone of a client.

I wasn't able to implement it in a better way than this:

```convertSelectedTimeZoneToClients() {
    let timeZoneInfo = {
        usersTimeZone: this.$rootScope.mtz.tz.guess(),
        utcOffset: this.formData.timeZone.offset,
        selectedDateTime: this.toJSONLocal(this.formData.sessionDate) + " " + this.formData.sessionTime 
    };

    let utcTime = this.$rootScope.mtz.utc(timeZoneInfo.selectedDateTime).utcOffset(timeZoneInfo.utcOffset).format("YYYY-MM-DD HH:mm");
    let convertedTime = this.$rootScope.mtz.tz(utcTime, timeZoneInfo.usersTimeZone).format("Z");
    return convertedTime;
}
```

So basically I am using ```usersTimeZone: this.$rootScope.mtz.tz.guess()```, guess() function to find out timezone from the browser.
Then I get values from datetime picker and dropdown and convert them to UTC value by using utcOffset.
At the end I want to convert that utc value to user timezone value.

I get object like this:



_d represent correct value after conversion. I have tried adding bunch of different .format() paterns on convertedTime variable, but I am not able to retrive time in this format: "YYYY-MM-DD HH:mm". I guess it works differentlly than when using .utcOffset() function.

Can anybody help me with this?

Comment: As general rule when using moment: all properties starting with `_` (like `_d`) are for [internal use](http://stackoverflow.com/a/28132227/4131048) and should not be used. Note that the code you showed is different from the code in the screenshot. Finally I think that could be useful to know what `toJSONLocal` returns (what is the value of `timeZoneInfo.selectedDateTime`?)

Here is the accepted answer: You don't need to guess the client time zone to convert to local time.  Just use the ```local``` function.

For example:

```moment.tz('2016-01-01 00:00', 'America/New_York').local().format('YYYY-MM-DD HH:mm')
```

For users located in the Pacific time zone, this converts from Eastern to Pacific and you get an output string of ```"2015-12-31 21:00"```.  For users in other time zones, the output would be different, as expected.

You don't need to format to a string and re-parse it, or manually manipulate the UTC offset either.  That is almost never warranted.

Comment for this answer: Thanks for the answer. Just one more question. When I am passing this value: 'America/New_York' How can I find it out? That is why I was using .guess(), only to find out the timezone of the user. @MattJohnson
Comment for this answer: I don't understand what you're asking.  In the example I gave, `America/New_York` is the *source* time zone - the time zone that the given value is "in".  You wouldn't want to guess that, or the whole thing would be no-op.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:11/03/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><angularjs><datetime><timezone><momentjs>', 'Title': 'Return date and time in ("YYYY-MM-DD HH:mm") format when using moment.js (timezone)', 'ViewCount': 3360, 'id': 40405057, 'user_AboutMe': "<p>.NET junior programmer on the very start of my carrier. I enjoy in learning new technologies, exploring what's out there, learn languages, tennis and to cook for my friends.</p>\n", 'user_AccountId': 3957175.0, 'user_DisplayName': 'nemo_87', 'user_Reputation': 4205.0, 'user_Views': 261.0, 'user_id': 3265282.0}}