我正在使用moment设置/更改我的日期对象的时区,而不是其他任何日期/时间值
这就是我目前正在做的事情:
const moment = require("moment-timezone");
const dateNew = moment.tz(accountDate, "US/Pacific");
这是accountDate值:Mon Jul 08 2019 06:05:22 GMT-0400 (Eastern Daylight Time)
我想将其更改为:Mon Jul 08 2019 06:05:22 GMT-0700 (Pacific Daylight Time)
,但是dateNew
仍处于EDT时区。
the output of `console.log(dateNew)` is :
Moment {_isAMomentObject: true, _i: Mon Jul 08 2019 06:05:22 GMT-0400 (Eastern Daylight Time), _isUTC: true, _pf: {…}, _locale: Locale, …}
_d: Sun Jul 07 2019 23:05:22 GMT-0400 (Eastern Daylight Time) {}
_i: Mon Jul 08 2019 06:05:22 GMT-0400 (Eastern Daylight Time) {}
_isAMomentObject: true
_isUTC: true
_isValid: true
_locale: Locale {_calendar: {…}, _longDateFormat: {…}, _invalidDate: "Invalid date", ordinal: ƒ, _dayOfMonthOrdinalParse: /\d{1,2}(th|st|nd|rd)/, …}
_offset: -420
_pf: {empty: false, unusedTokens: Array(0), unusedInput: Array(0), overflow: -2, charsLeftOver: 0, …}
_z: Zone {name: "US/Pacific", abbrs: Array(186), untils: Array(186), offsets: Array(186), population: 15000000}
__proto__: Object
但是console.log(new Date(dateNew))
提供以下输出。
Mon Jul 08 2019 06:05:22 GMT-0400 (Eastern Daylight Time)
请帮助。谢谢
答案 0 :(得分:1)
您确实在更改时区,一切正常,仅添加format(),我还没有足够的声誉对此发表评论,大声笑,但是为什么要使用Date(),请坚持一下矩( )
const dateNew = moment.tz(accountDate, "US/Pacific").format();
console.log(dateNew)
答案 1 :(得分:0)
因为您包含了reactjs标签。我会以react.js的方式给你一个答案。您可以安装名为moment-timezone
的npm软件包来解决此问题。也请查看他们的docs。这是React中的完整示例
import React, { Component } from "react";
import ReactDOM from "react-dom";
import moment from "moment-timezone";
import "./styles.css";
class App extends Component {
render() {
const _date = "Mon Jul 08 2019 06:05:22 GMT-0400 (Eastern Daylight Time)";
const result = moment.tz(_date, "US/Pacific");
return (
<div>
<p>Origin Date : {_date}</p>
<p>
Converted Date : {result.format()} {result.tz()}
</p>
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
上面的代码将得到这样的结果
答案 2 :(得分:0)
我将其发布为另一个答案,所以我可以将其添加为代码@Tanu
class TestClassTest {
var testInstance = TestClass()
@Test
fun `Test with default dispatchers should fail`() = runBlocking {
val valueToModify = testInstance.runAsync().await()
assertTrue(valueToModify)
}
@Test
fun `Test with dispatchers replaced by Unconfined should pass`() = runBlocking {
testInstance.DefaultDispatcher = Dispatchers.Unconfined
testInstance.IODispatcher = Dispatchers.Unconfined
val valueToModify = testInstance.runAsync().await()
assertTrue(valueToModify)
}
@Test
fun `I need to also test some functions that use suspend coroutine - How can I do that?`() = runBlocking {
testInstance.DefaultDispatcher = Dispatchers.Unconfined
testInstance.IODispatcher = Dispatchers.Unconfined
val valueToModify = testInstance.runSuspendCoroutine().await()
assertTrue(valueToModify)//fails
}
}
class TestClass {
var DefaultDispatcher: CoroutineContext = Dispatchers.Default
var IODispatcher: CoroutineContext = Dispatchers.IO
val viewModelScope = CoroutineScope(DefaultDispatcher)
fun runAsync(): Deferred<Boolean> {
return viewModelScope.async(DefaultDispatcher) {
withContext(IODispatcher) {
sleep(1000)
true
}
}
}
fun runSuspendCoroutine(): Deferred<Boolean> {
return viewModelScope.async(DefaultDispatcher) {
suspendCoroutine<Boolean> {
Thread {
sleep(1000)
//long running operation calls listener from background thread
it.resume(true)
}.start()
}
}
}
}
此外,您可以像这样添加更改缩写,并在括号中添加一些其他方括号:
const accountDate = 'Mon Jul 08 2019 06:05:22 GMT-0400 (Eastern Daylight Time)'
const dateNew = moment(accountDate).tz('US/Pacific')
console.log(dateNew.format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ zz'))
//OUTPUT: Mon Jul 08 2019 03:05:22 GMT-0700 PDT
因此输出现在为:
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;
}