admin管理员组文章数量:1026989
Suppose I have a string like "01/22/2014 2:07:00 PM -08:00".
I want to
- a) Format it in ISO 8601 with time offsets from UTC , so it bees "2014-01-22T14:07:00-08:00"
- b) Strip out time offset part so it bees "01/22/2014 2:07:00 PM" [and then format it in ISO 8601 so it bees "2014-01-22T14:07:00"]
Sure I can use JavaScript string functions (and regular expressions), but it seems to be a better approach to use JavaScript Date() object facilities or Moment.js. Neither work, however. Both automatically convert dates to a current system timezone (-05:00 for me), so 2:07 PM bees 5:07 PM. I found two ways of doing that "strip out time offset then format" task, but both looks ugly and brittle:
var mydateTime = "01/22/2014 2:07:00 PM -08:00";
// strip out time offset part using substring() so that Moment.js
// would think time is specified in a current zone
var myNewDateTime1 = moment(mydateTime.substring(0, mydateTime.length - 7)).format("YYYY-MM-DDTHH:mm:ss")
// or probably even worse trick - strip out time offset part using format
var myNewDateTime2 = moment(mydateTime, "MM/DD/YYYY h:mm:ss A").format("YYYY-MM-DDTHH:mm:ss")
I understand that JavaScript Date() object is not designed to preserve a time zone, but doesn't more elegant and stable solution exist for a) and b) ?
Suppose I have a string like "01/22/2014 2:07:00 PM -08:00".
I want to
- a) Format it in ISO 8601 with time offsets from UTC http://goo.gl/JTfAZq, so it bees "2014-01-22T14:07:00-08:00"
- b) Strip out time offset part so it bees "01/22/2014 2:07:00 PM" [and then format it in ISO 8601 so it bees "2014-01-22T14:07:00"]
Sure I can use JavaScript string functions (and regular expressions), but it seems to be a better approach to use JavaScript Date() object facilities or Moment.js. Neither work, however. Both automatically convert dates to a current system timezone (-05:00 for me), so 2:07 PM bees 5:07 PM. I found two ways of doing that "strip out time offset then format" task, but both looks ugly and brittle:
var mydateTime = "01/22/2014 2:07:00 PM -08:00";
// strip out time offset part using substring() so that Moment.js
// would think time is specified in a current zone
var myNewDateTime1 = moment(mydateTime.substring(0, mydateTime.length - 7)).format("YYYY-MM-DDTHH:mm:ss")
// or probably even worse trick - strip out time offset part using format
var myNewDateTime2 = moment(mydateTime, "MM/DD/YYYY h:mm:ss A").format("YYYY-MM-DDTHH:mm:ss")
I understand that JavaScript Date() object is not designed to preserve a time zone, but doesn't more elegant and stable solution exist for a) and b) ?
Share Improve this question edited Feb 20, 2014 at 17:32 vkelman asked Jan 24, 2014 at 18:38 vkelmanvkelman 1,6311 gold badge17 silver badges25 bronze badges1 Answer
Reset to default 7I think you are looking for moment.ParseZone
. It parses the moment AND preserves the time zone offset that was in the string, instead of converting it to the browser's local time zone.
Also, your myDateTime
variable doesn't match what you were asking about. If you do indeed already have a full ISO8601 extended with time zone offfset, then it is like this:
var m = moment.parseZone("2014-01-22T14:07:00-08:00");
Or if it's like you originally, showed, then like this:
var m = moment("01/22/2014 2:07:00 PM -08:00",
"MM/DD/YYYY h:mm:ss A Z").parseZone();
From there, you can format it however you like:
var s = m.format("YYYY-MM-DDTHH:mm:ss");
Suppose I have a string like "01/22/2014 2:07:00 PM -08:00".
I want to
- a) Format it in ISO 8601 with time offsets from UTC , so it bees "2014-01-22T14:07:00-08:00"
- b) Strip out time offset part so it bees "01/22/2014 2:07:00 PM" [and then format it in ISO 8601 so it bees "2014-01-22T14:07:00"]
Sure I can use JavaScript string functions (and regular expressions), but it seems to be a better approach to use JavaScript Date() object facilities or Moment.js. Neither work, however. Both automatically convert dates to a current system timezone (-05:00 for me), so 2:07 PM bees 5:07 PM. I found two ways of doing that "strip out time offset then format" task, but both looks ugly and brittle:
var mydateTime = "01/22/2014 2:07:00 PM -08:00";
// strip out time offset part using substring() so that Moment.js
// would think time is specified in a current zone
var myNewDateTime1 = moment(mydateTime.substring(0, mydateTime.length - 7)).format("YYYY-MM-DDTHH:mm:ss")
// or probably even worse trick - strip out time offset part using format
var myNewDateTime2 = moment(mydateTime, "MM/DD/YYYY h:mm:ss A").format("YYYY-MM-DDTHH:mm:ss")
I understand that JavaScript Date() object is not designed to preserve a time zone, but doesn't more elegant and stable solution exist for a) and b) ?
Suppose I have a string like "01/22/2014 2:07:00 PM -08:00".
I want to
- a) Format it in ISO 8601 with time offsets from UTC http://goo.gl/JTfAZq, so it bees "2014-01-22T14:07:00-08:00"
- b) Strip out time offset part so it bees "01/22/2014 2:07:00 PM" [and then format it in ISO 8601 so it bees "2014-01-22T14:07:00"]
Sure I can use JavaScript string functions (and regular expressions), but it seems to be a better approach to use JavaScript Date() object facilities or Moment.js. Neither work, however. Both automatically convert dates to a current system timezone (-05:00 for me), so 2:07 PM bees 5:07 PM. I found two ways of doing that "strip out time offset then format" task, but both looks ugly and brittle:
var mydateTime = "01/22/2014 2:07:00 PM -08:00";
// strip out time offset part using substring() so that Moment.js
// would think time is specified in a current zone
var myNewDateTime1 = moment(mydateTime.substring(0, mydateTime.length - 7)).format("YYYY-MM-DDTHH:mm:ss")
// or probably even worse trick - strip out time offset part using format
var myNewDateTime2 = moment(mydateTime, "MM/DD/YYYY h:mm:ss A").format("YYYY-MM-DDTHH:mm:ss")
I understand that JavaScript Date() object is not designed to preserve a time zone, but doesn't more elegant and stable solution exist for a) and b) ?
Share Improve this question edited Feb 20, 2014 at 17:32 vkelman asked Jan 24, 2014 at 18:38 vkelmanvkelman 1,6311 gold badge17 silver badges25 bronze badges1 Answer
Reset to default 7I think you are looking for moment.ParseZone
. It parses the moment AND preserves the time zone offset that was in the string, instead of converting it to the browser's local time zone.
Also, your myDateTime
variable doesn't match what you were asking about. If you do indeed already have a full ISO8601 extended with time zone offfset, then it is like this:
var m = moment.parseZone("2014-01-22T14:07:00-08:00");
Or if it's like you originally, showed, then like this:
var m = moment("01/22/2014 2:07:00 PM -08:00",
"MM/DD/YYYY h:mm:ss A Z").parseZone();
From there, you can format it however you like:
var s = m.format("YYYY-MM-DDTHH:mm:ss");
本文标签: javascriptStrip Time Zone from a string using JavaScriptyMomentjsetcStack Overflow
版权声明:本文标题:javascript - Strip Time Zone from a string using JavaScripty, Moment.js, etc? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1744005280a2065273.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论