Golang time.Time.Month()函数及其示例

Golang time.Time.Month()函数及其示例

在Go语言中,time包提供了查看和确定时间的功能。Go语言中的 Time.Month() 函数用于查找由t提供的年份的月份。此外,该功能是在time包下定义的。在这里,您需要导入“time”包才能使用这些函数。

句法:

func (t Time) Month() Month

在此,”t”是指定时间。

返回值: 返回由“t”提供的年份的月份。

示例1:

// Golang program to illustrate the usage of
// Time.Month() function
// 包含主要程序
package main

// 引入fmt和time
import "fmt"
import "time"

// 调用main函数
func main() {

 // 在UTC中声明t
 t := time.Date(2019, 6, 5,
  11, 51, 04, 0, time.UTC)

 // 调用Month方法
 month := t.Month()

 // 按规定打印月份
 fmt.Printf("指定年份的月份是:%v\n", month)
} 

输出:

指定年份的月份是:六月

示例2:

// Golang program to illustrate the usage of
// Time.Month() function
// 包含主要程序
package main

// 引入fmt和time
import "fmt"
import "time"

// 调用main函数
func main() {

 // 在UTC中声明t
 t := time.Date(2019, 23, 5,
  11, 51, 04, 0, time.UTC)

 // 调用Month方法
 month := t.Month()

 // 按规定打印月份
 fmt.Printf("指定的年份的月份是:%v\n", month)
} 

输出:

指定的年份的月份是:十一月

这里,上述代码中给出的月份为整数,超出了常规范围,但在转换时经过了规范化。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程