0

I am trying to format a timezone based

How can i convert a JS time into these formats?

"Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)"

"September 24th 2015, 2:00:00 pm UTC-07:00"

"2015-09-24 14:00:00 GMT-0700"

"Sept 24 2015 14:00:00 GMT-0700"

"Thu Sep 24 2015 14:00:00 GMT-0700 (Pacific Daylight Time)"

Converting into any of it would help.

6
  • 1
    Have you tried by your-self? Can you post some of your code to see what you are missing or if there is some error? Commented Nov 9, 2016 at 10:40
  • I was able to get the 3rd one using this format: "YYYY-MM-DD HH:mm:ss zZZ" Commented Nov 9, 2016 at 11:44
  • Ok, can you post your code, please? It would be very useful to start from what you tried... Commented Nov 9, 2016 at 11:46
  • var time = "2016-11-09 15:38:00", zone = "America/Chicago", format = "YYYY-MM-DD HH:mm:ss zZZ"; moment.tz(time,zone).utc().format(format) Commented Nov 9, 2016 at 12:02
  • Ok, but you asked "Converting into any of it would help." but you are able to convert in the 3rd one... It answers your own question. Am I correct? Commented Nov 9, 2016 at 14:35

2 Answers 2

1

You can use tokens listed in the format documentation, as shown in the following snippet.

Use square brackets [] to add characters that should be escaped (GMT and UTC in the example, if you need current zone abbreviation use the z token).

Note that as the moment-timezone docs says:

Moment.js also provides a hook for the long form time zone name. Because these strings are generally localized, Moment Timezone does not provide any long names for zones.

To provide long form names, you can override moment.fn.zoneName and use the zz token.

You can find in the snippet an example of providing long names for zones.

var time = "2016-11-09 15:38:00", zone = "America/Chicago";

var m = moment.tz(time,zone);

console.log(m.format('ddd MMM D YYYY HH:mm:ss [GMT]ZZ (z)'));
console.log(m.format('MMMM Do YYYY, h:mm:ss a [UTC]ZZ'));
console.log(m.format('YYYY-MM-DD HH:mm:ss [GMT]ZZ'));
console.log(m.format('MMM D YYYY HH:mm:ss [GMT]ZZ'));

// Add long names for sample zones
var abbrs = {
    EST : 'Eastern Standard Time',
    EDT : 'Eastern Daylight Time',
    CST : 'Central Standard Time',
    CDT : 'Central Daylight Time',
    MST : 'Mountain Standard Time',
    MDT : 'Mountain Daylight Time',
    PST : 'Pacific Standard Time',
    PDT : 'Pacific Daylight Time',
};

moment.fn.zoneName = function () {
    var abbr = this.zoneAbbr();
    return abbrs[abbr] || abbr;
};

console.log(m.format('ddd MMM D YYYY HH:mm:ss [GMT]ZZ (zz)'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.7/moment-timezone-with-data-2010-2020.min.js"></script>

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

Comments

1

I was able to make third one using this snippet

var time = "2016-11-09 15:38:00", 
    zone = "America/Chicago", 
    format = "YYYY-MM-DD HH:mm:ss zZZ"; 

moment.tz(time,zone).utc().format(format)

Comments

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.