Skip to content

Commit

Permalink
moment#371 don't add timezone to string + format in moment.utc if it …
Browse files Browse the repository at this point in the history
…already has a timezone
  • Loading branch information
timrwood committed Jul 25, 2012
1 parent 65db92e commit cfa347e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
11 changes: 8 additions & 3 deletions moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -664,9 +664,14 @@
if (isArray(input)) {
return new Moment(dateFromArray(input, true), true);
}
return (format && input) ?
moment(input + ' +0000', format + ' Z').utc() :
moment(input && isoRegex.exec(input) && !parseTokenTimezone.exec(input) ? input + '+0000' : input).utc();
// if we don't have a timezone, we need to add one to trigger parsing into utc
if (typeof input === 'string' && !parseTokenTimezone.exec(input)) {
input += ' +0000';
if (format) {
format += ' Z';
}
}
return moment(input, format).utc();
};

// creating with unix timestamp (in seconds)
Expand Down
5 changes: 4 additions & 1 deletion test/moment/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,17 @@ exports.create = {
},

"string with format (timezone offset)" : function(test) {
test.expect(3);
test.expect(4);
var a = new Date(Date.UTC(2011, 0, 1, 1));
var b = moment('2011 1 1 0 -01:00', 'YYYY MM DD HH Z');
test.equal(a.getHours(), b.hours(), 'date created with utc == parsed string with timezone offset');
test.equal(+a, +b, 'date created with utc == parsed string with timezone offset');
var c = moment('2011 2 1 10 -05:00', 'YYYY MM DD HH Z');
var d = moment('2011 2 1 8 -07:00', 'YYYY MM DD HH Z');
test.equal(c.hours(), d.hours(), '10 am central time == 8 am pacific time');
var e = moment.utc('Fri, 20 Jul 2012 17:15:00', 'ddd, DD MMM YYYY HH:mm:ss');
var f = moment.utc('Fri, 20 Jul 2012 10:15:00 -0700', 'ddd, DD MMM YYYY HH:mm:ss ZZ');
test.equal(e.hours(), f.hours(), 'parse timezone offset in utc');
test.done();
},

Expand Down

0 comments on commit cfa347e

Please sign in to comment.