如何为日期选择器添加时区?
这是我现在的代码:输出格式:像这样2018-04-10T04:24:24.443980 + 08:00
@available(iOS 11.0, *)
func donePressed() {
let formatter = ISO8601DateFormatter()
if #available(iOS 11.0, *) {
formatter.formatOptions.insert(.withFractionalSeconds)
} else {
// Fallback on earlier versions
}
let dateString = formatter.string(from: picker.date)
choreSchedule.text = "\(dateString)"
}
答案 0 :(得分:0)
假设您只是尝试从用户的系统设置时区,您可以在设置UIDatePicker时使用TimeZone.current属性(或选择您选择的时区):
let picker = UIDatePicker()
picker.timeZone = .current
或者如果你有一个可变的时区:
func donePressed(timeZone: TimeZone) {
let formatter = ISO8601DateFormatter()
if #available(iOS 11.0, *) {
formatter.formatOptions.insert(.withFractionalSeconds)
} else {
// Fallback on earlier versions
}
picker.timeZone = timeZone
let dateString = formatter.string(from: picker.date)
choreSchedule.text = "\(dateString)"
}
答案 1 :(得分:0)
Testing started at 6:24 PM ...
04/09 18:24:17: Launching CoordinatorActivityT...
$ adb push /Users/plentz/Projects/example/android/main/build/outputs/apk/debug/main-debug.apk /data/local/tmp/foo.example
pkg: /data/local/tmp/foo.example
Success
$ adb push /Users/plentz/Projects/example/android/main/build/outputs/apk/androidTest/debug/main-debug-androidTest.apk /data/local/tmp/foo.example.test
$ adb shell pm install -t -r "/data/local/tmp/foo.example.test"
pkg: /data/local/tmp/foo.example.test
Success
Running tests
$ adb shell am instrument -w -r -e debug false -e class 'foo.example.ui.activity.CoordinatorActivityTest' foo.example.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
java.lang.NoClassDefFoundError: android.support.test.espresso.base.BaseLayerModule_ProvideFailureHanderFactory
at android.support.test.espresso.DaggerBaseLayerComponent.initialize(DaggerBaseLayerComponent.java:66)
at android.support.test.espresso.DaggerBaseLayerComponent.<init>(DaggerBaseLayerComponent.java:54)
at android.support.test.espresso.DaggerBaseLayerComponent.<init>(DaggerBaseLayerComponent.java:40)
at android.support.test.espresso.DaggerBaseLayerComponent$Builder.build(DaggerBaseLayerComponent.java:123)
at android.support.test.espresso.DaggerBaseLayerComponent.create(DaggerBaseLayerComponent.java:62)
at android.support.test.espresso.GraphHolder.baseLayer(GraphHolder.java:42)
at android.support.test.espresso.Espresso.<clinit>(Espresso.java:51)
at foo.example.ui.activity.CoordinatorActivityTest.coordinatorActivityTest(CoordinatorActivityTest.java:37)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
Tests ran to completion.
对我不起作用
picker.timeZone = .current
。
对于给定的问题:
formatter.timeZone = .current
此外,要设置其他时区,您可以将func donePressed() {
let formatter = ISO8601DateFormatter()
formatter.timeZone = .current
if #available(iOS 11.0, *) {
formatter.formatOptions.insert(.withFractionalSeconds)
} else {
// Fallback on earlier versions
}
let dateString = formatter.string(from: picker.date)
choreSchedule.text = "\(dateString)"
}
替换为formatter.timeZone = .current
标识符“EST”是特定时区的缩写。有关可用时区列表,您可以尝试:
formatter.timeZone = TimeZone(identifier: "EST")