菜鸟教程小白 发表于 2022-12-11 18:28:44

ios - 来自字符串时区的日期不使用 DateFormatter


                                            <p><p>代码:</p>

<pre><code>let isoDate = &#34;1981-02-20T10:44:00+0800&#34;
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = &#34;yyyy-MM-dd&#39;T&#39;HH:mm:ssZ&#34;

var localTimeZoneAbbreviation: String { return TimeZone.current.abbreviation() ?? &#34;&#34; }

let date = dateFormatter.date(from: isoDate)!
dateFormatter.string(from: date)
</code></pre>

<p>输出:
<a href="/image/18Yjm.png" rel="noreferrer noopener nofollow"><img src="/image/18Yjm.png" alt="enter image description here"/></a> </p>

<p>为什么日期输出时区是 +07:30 而不是日期字符串和 TimeZone.current 中定义的 +0800?谁能解释一下?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>时区格式不是<code>Z</code>而是<code>ZZZ</code></p>

<pre><code>dateFormatter.dateFormat = &#34;yyyy-MM-dd&#39;T&#39;HH:mm:ssZZZ&#34;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 来自字符串时区的日期不使用 DateFormatter,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45584841/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45584841/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 来自字符串时区的日期不使用 DateFormatter