<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>swift &#8211; 隨心所欲</title>
	<atom:link href="https://doeverythingiwant.com/tag/swift/feed/" rel="self" type="application/rss+xml" />
	<link>https://doeverythingiwant.com</link>
	<description>iOS Developer 的隨筆記錄</description>
	<lastBuildDate>Sun, 20 Oct 2024 14:31:29 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1</generator>

<image>
	<url>https://doeverythingiwant.com/wp-content/uploads/2025/08/cropped-0802.png</url>
	<title>swift &#8211; 隨心所欲</title>
	<link>https://doeverythingiwant.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>[iOS] About Date</title>
		<link>https://doeverythingiwant.com/ios-about-date/</link>
					<comments>https://doeverythingiwant.com/ios-about-date/#respond</comments>
		
		<dc:creator><![CDATA[艾普利]]></dc:creator>
		<pubDate>Wed, 07 Sep 2022 14:45:11 +0000</pubDate>
				<category><![CDATA[寫程式]]></category>
		<category><![CDATA[Date Convert]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[swift]]></category>
		<guid isPermaLink="false">https://doeverythingiwant.com/?p=95</guid>

					<description><![CDATA[日期格式總是讓人覺得頭痛，以下整理了幾個常用的日期格式轉換 &#8230;]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">日期格式總是讓人覺得頭痛，以下整理了幾個常用的日期格式轉換</p>



<span id="more-95"></span>



<p class="has-large-font-size wp-block-paragraph"><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Target Date String : “2021/06/20”</strong></mark></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">String to Date</h2>



<div class="hcb_wrap"><pre class="prism line-numbers lang-swift" data-lang="Swift"><code>func convertStringToDate(str: String) -&gt; Date? {
    let formatter = DateFormatter()
    formatter.locale = Locale.current
    formatter.timeZone = TimeZone.current
    formatter.dateFormat = &quot;yyyy/mm/dd&quot;
    return formatter.date(from: str)
}</code></pre></div>



<h2 class="wp-block-heading">Date to String</h2>



<div class="hcb_wrap"><pre class="prism line-numbers lang-swift" data-lang="Swift"><code>func convertDateToString(date: Date) -&gt; String? {
    let formatter = DateFormatter()
    formatter.locale = Locale.current
    formatter.timeZone = TimeZone.current
    formatter.dateFormat = &quot;yyyy/mm/dd&quot;
    return formatter.string(from: date)
}</code></pre></div>



<h2 class="wp-block-heading">Relative Date</h2>



<div class="hcb_wrap"><pre class="prism line-numbers lang-swift" data-lang="Swift"><code>func relativeDate(date: Date) -&gt; String {
    let formatter = RelativeDateTimeFormatter()
    formatter.locale = Locale.current
    return formatter.localizedString(for: date, relativeTo: Date())
}
//return value will be &quot;2 hour age&quot; or &quot;2 day age&quot;</code></pre></div>



<h2 class="wp-block-heading">Convert ISO8601</h2>



<ul class="wp-block-list">
<li>year, month, day, as “XXXX-XX-XX”</li>



<li>“T” as a separator</li>



<li>hour, minute, seconds, milliseconds, as “XX:XX:XX.XXX”.</li>



<li>“Z” as a zone designator for zero offset, a.k.a. UTC, GMT, Zulu time.</li>
</ul>



<p class="wp-block-paragraph" id="a3fc"><strong><mark style="background-color:rgba(0, 0, 0, 0);color:#1549b9" class="has-inline-color">Format : 2021–06–20T10:00:05Z</mark></strong></p>



<p class="wp-block-paragraph" id="fe04"><code>yyyy-MM-dd'T'HH:mm:ss'Z'</code></p>



<h3 class="wp-block-heading">String to Date</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-swift" data-lang="Swift"><code>func convertStringToISO8601Date(str: String) -&gt; Date? {
   let dateFormatter = ISO8601DateFormatter()
   var date = dateFormatter.date(from: str)
   return dateFormatter.date(from: self)
}</code></pre></div>



<h3 class="wp-block-heading">Date to String</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-swift" data-lang="Swift"><code>func convertStringToISO8601Date(date: Date) -&gt; String? {
   let dateFormatter = ISO8601DateFormatter()
   var date = dateFormatter.date(from: str)
   return dateFormatter.string(from: date)
}</code></pre></div>



<p class="wp-block-paragraph" id="42c8"><strong><mark style="background-color:rgba(0, 0, 0, 0);color:#1549b9" class="has-inline-color">Format : 2021–06–20T10:00:05.000Z</mark></strong></p>



<p class="wp-block-paragraph" id="386b"><code>yyyy-MM-dd'T'HH:mm:ss.SSS'Z'</code></p>



<h3 class="wp-block-heading">String to Date</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-swift" data-lang="Swift"><code>func convertStringToISO8601Date(str: String) -&gt; Date? {
   let dateFormatter = ISO8601DateFormatter()
   dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
   var date = dateFormatter.date(from: str)
   return dateFormatter.date(from: self)
}</code></pre></div>



<h3 class="wp-block-heading">Date to String</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-swift" data-lang="Swift"><code>func convertStringToISO8601Date(date: Date) -&gt; String? {
   let dateFormatter = ISO8601DateFormatter()
   dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
   var date = dateFormatter.date(from: str)
   return dateFormatter.string(from: date)
}</code></pre></div>



<h2 class="wp-block-heading">Combination</h2>



<h3 class="wp-block-heading">String to Date</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-swift" data-lang="Swift"><code>func convertStringToISO8601Date(str: String) -&gt; Date? {
    let dateFormatter = ISO8601DateFormatter()
    var date = dateFormatter.date(from: self)
    if date == nil {
        dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
        date = dateFormatter.date(from: str)
    }
    return date
}</code></pre></div>



<p class="wp-block-paragraph">祝大家 Coding 愉快!!!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://doeverythingiwant.com/ios-about-date/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
