-
Notifications
You must be signed in to change notification settings - Fork 8
/
time-formatting-parsing.html
203 lines (159 loc) · 9.85 KB
/
time-formatting-parsing.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example 中文版: 时间的格式化和解析</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'epoch';
}
if (e.key == "ArrowRight") {
window.location.href = 'random-numbers';
}
}
</script>
<body>
<div class="example" id="time-formatting-parsing">
<h2><a href="./">Go by Example 中文版</a>: 时间的格式化和解析</h2>
<table>
<tr>
<td class="docs">
<p>Go 支持通过基于描述模板的时间格式化与解析。</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="https://play.studygolang.com/p/LVJJMMUok4D"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kn">import</span> <span class="p">(</span>
<span class="s">"fmt"</span>
<span class="s">"time"</span>
<span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">p</span> <span class="o">:=</span> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>这是一个遵循 RFC3339,
并使用对应的 <code>布局</code>(layout)常量进行格式化的基本例子。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">t</span> <span class="o">:=</span> <span class="nx">time</span><span class="p">.</span><span class="nf">Now</span><span class="p">()</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">t</span><span class="p">.</span><span class="nf">Format</span><span class="p">(</span><span class="nx">time</span><span class="p">.</span><span class="nx">RFC3339</span><span class="p">))</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>时间解析使用与 <code>Format</code> 相同的布局值。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">t1</span><span class="p">,</span> <span class="nx">e</span> <span class="o">:=</span> <span class="nx">time</span><span class="p">.</span><span class="nf">Parse</span><span class="p">(</span>
<span class="nx">time</span><span class="p">.</span><span class="nx">RFC3339</span><span class="p">,</span>
<span class="s">"2012-11-01T22:08:41+00:00"</span><span class="p">)</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">t1</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>Format</code> 和 <code>Parse</code> 使用基于例子的布局来决定日期格式,
一般你只要使用 <code>time</code> 包中提供的布局常量就行了,但是你也可以实现自定义布局。
布局时间必须使用 <code>Mon Jan 2 15:04:05 MST 2006</code> 的格式,
来指定 格式化/解析给定时间/字符串 的布局。
时间一定要遵循:2006 为年,15 为小时,Monday 代表星期几等规则。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nf">p</span><span class="p">(</span><span class="nx">t</span><span class="p">.</span><span class="nf">Format</span><span class="p">(</span><span class="s">"3:04PM"</span><span class="p">))</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">t</span><span class="p">.</span><span class="nf">Format</span><span class="p">(</span><span class="s">"Mon Jan _2 15:04:05 2006"</span><span class="p">))</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">t</span><span class="p">.</span><span class="nf">Format</span><span class="p">(</span><span class="s">"2006-01-02T15:04:05.999999-07:00"</span><span class="p">))</span>
<span class="nx">form</span> <span class="o">:=</span> <span class="s">"3 04 PM"</span>
<span class="nx">t2</span><span class="p">,</span> <span class="nx">e</span> <span class="o">:=</span> <span class="nx">time</span><span class="p">.</span><span class="nf">Parse</span><span class="p">(</span><span class="nx">form</span><span class="p">,</span> <span class="s">"8 41 PM"</span><span class="p">)</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">t2</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>对于纯数字表示的时间(时间戳),
您还可以将标准字符串格式与提取时间值的一部分一起使用。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">"%d-%02d-%02dT%02d:%02d:%02d-00:00\n"</span><span class="p">,</span>
<span class="nx">t</span><span class="p">.</span><span class="nf">Year</span><span class="p">(),</span> <span class="nx">t</span><span class="p">.</span><span class="nf">Month</span><span class="p">(),</span> <span class="nx">t</span><span class="p">.</span><span class="nf">Day</span><span class="p">(),</span>
<span class="nx">t</span><span class="p">.</span><span class="nf">Hour</span><span class="p">(),</span> <span class="nx">t</span><span class="p">.</span><span class="nf">Minute</span><span class="p">(),</span> <span class="nx">t</span><span class="p">.</span><span class="nf">Second</span><span class="p">())</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>当输入的时间格式不正确时,<code>Parse</code> 会返回一个解析错误。</p>
</td>
<td class="code">
<pre class="chroma">
<span class="nx">ansic</span> <span class="o">:=</span> <span class="s">"Mon Jan _2 15:04:05 2006"</span>
<span class="nx">_</span><span class="p">,</span> <span class="nx">e</span> <span class="p">=</span> <span class="nx">time</span><span class="p">.</span><span class="nf">Parse</span><span class="p">(</span><span class="nx">ansic</span><span class="p">,</span> <span class="s">"8:41PM"</span><span class="p">)</span>
<span class="nf">p</span><span class="p">(</span><span class="nx">e</span><span class="p">)</span>
<span class="p">}</span>
</pre>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
</td>
<td class="code">
<pre class="chroma"><span class="gp">$</span> go run time-formatting-parsing.go
<span class="go">2014-04-15T18:00:15-07:00
</span><span class="go">2012-11-01 22:08:41 +0000 +0000
</span><span class="go">6:00PM
</span><span class="go">Tue Apr 15 18:00:15 2014
</span><span class="go">2014-04-15T18:00:15.161182-07:00
</span><span class="go">0000-01-01 20:41:00 +0000 UTC
</span><span class="go">2014-04-15T18:00:15-00:00
</span><span class="go">parsing time "8:41PM" as "Mon Jan _2 15:04:05 2006": ...</span></pre>
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="random-numbers">随机数</a>
</p>
<p class="footer">
<a href="https://twitter.com/mmcgrana">@mmcgrana</a> 和<a href="https://eli.thegreenplace.net">Eli Bendersky</a>编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a> </p>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A p :\u003D fmt.Println\u000A');codeLines.push(' t :\u003D time.Now()\u000A p(t.Format(time.RFC3339))\u000A');codeLines.push(' t1, e :\u003D time.Parse(\u000A time.RFC3339,\u000A \"2012-11-01T22:08:41+00:00\")\u000A p(t1)\u000A');codeLines.push(' p(t.Format(\"3:04PM\"))\u000A p(t.Format(\"Mon Jan _2 15:04:05 2006\"))\u000A p(t.Format(\"2006-01-02T15:04:05.999999-07:00\"))\u000A form :\u003D \"3 04 PM\"\u000A t2, e :\u003D time.Parse(form, \"8 41 PM\")\u000A p(t2)\u000A');codeLines.push(' fmt.Printf(\"%d-%02d-%02dT%02d:%02d:%02d-00:00\\n\",\u000A t.Year(), t.Month(), t.Day(),\u000A t.Hour(), t.Minute(), t.Second())\u000A');codeLines.push(' ansic :\u003D \"Mon Jan _2 15:04:05 2006\"\u000A _, e \u003D time.Parse(ansic, \"8:41PM\")\u000A p(e)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>