英文:
Go printing date to console
问题
我正在尝试将月份、日期和年份分别打印到控制台。
我需要能够单独访问日期的每个部分。我可以使用"time"包中的time.now()
获取整个日期,但之后我卡住了。
请问有人可以告诉我我错在哪里吗?
英文:
I'm trying to pint the month, day, and year, separately to the console.
I need to be able to access each section of the date individually. I can get the whole thing using time.now()
from the "time" package but I'm stuck after that.
Can anyone show me where I am going wrong please?
答案1
得分: 31
你的代码已经非常接近了 time.Now()
返回的是一个 Time
类型的值,你可以查看包文档 这里 来了解可以调用的一些方法(如果想快速浏览,请点击 这里 并查看 type Time
部分)。要获取你提到的每个属性,你可以这样做:
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println(t.Month())
fmt.Println(t.Day())
fmt.Println(t.Year())
}
如果你想将 Month
打印为整数,可以使用 Printf
函数:
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Printf("%d\n", t.Month())
}
英文:
You're actually pretty close Then return value from time.Now()
is a Time
type, and looking at the package docs here will show you some of the methods you can call (for a quicker overview, go here and look under type Time
). To get each of the attributes you mention above, you can do this:
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println(t.Month())
fmt.Println(t.Day())
fmt.Println(t.Year())
}
If you are interested in printing the Month
as an integer, you can use the Printf
function:
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Printf("%d\n", t.Month())
}
答案2
得分: 2
Day, Month and Year可以从time.Time
类型中使用Date()
方法提取。它将返回day和year的整数值,以及month的time.Month
类型。你也可以使用Clock()
方法提取Hour、Minute和Second的值,它将返回所有结果的整数值。
例如:
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
y, mon, d := t.Date()
h, m, s := t.Clock()
fmt.Println("Year: ", y)
fmt.Println("Month: ", mon)
fmt.Println("Day: ", d)
fmt.Println("Hour: ", h)
fmt.Println("Minute: ", m)
fmt.Println("Second: ", s)
}
请记住,Month变量(mon
)以time.Month
类型返回,而不是字符串或整数。你仍然可以使用fmt.Print()
打印它,因为它有一个String()
方法。
英文:
Day, Month and Year can be extracted from a time.Time
type with the Date()
method. It will return ints for both day and year, and a time.Month
for the month. You can also extract the Hour, Minute and Second values with the Clock()
method, which returns ints for all results.
For example:
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
y, mon, d := t.Date()
h, m, s := t.Clock()
fmt.Println("Year: ", y)
fmt.Println("Month: ", mon)
fmt.Println("Day: ", d)
fmt.Println("Hour: ", h)
fmt.Println("Minute: ", m)
fmt.Println("Second: ", s)
}
Please remember that the Month variable (mon
) is returned as a time.Month
, and not as a string, or an int. You can still print it with fmt.Print()
as it has a String()
method.
答案3
得分: 0
你可以直接解析字符串以获取年、月和日。
package main
import (
"fmt"
"strings"
)
func main() {
currTime := time.Now()
date := strings.Split(currTime.String(), " ")[0]
splits := strings.Split(date, "-")
year := splits[0]
month := splits[1]
day := splits[2]
fmt.Printf("%s-%s-%s\n", year, month, day)
}
请注意,这是一个Go语言的代码示例,用于从当前时间中提取年、月和日。
英文:
You can just parse the string to get year, month, & day.
package main
import (
"fmt"
"strings"
)
func main() {
currTime := time.Now()
date := strings.Split(currTime.String(), " ")[0]
splits := strings.Split(date, "-")
year := splits[0]
month := splits[1]
day := splits[2]
fmt.Printf("%s-%s-%s\n", year, month, day)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论