GO 时间相关函数

package main

import(
	"fmt"
	"time"
)
func main () {
	now := time.Now();
	fmt.Printf("now=%v, now type %T\n", now, now)

	fmt.Println("年=",now.Year());
	fmt.Println("月=",now.Month());//英文月份
	fmt.Println("月=",int(now.Month()));//转成整形
	fmt.Println("日=",now.Day());
	fmt.Println("时=",now.Hour());
	fmt.Println("分=",now.Minute());
	fmt.Println("秒=",now.Second());

	fmt.Printf("%02d-%02d-%02d %02d:%02d:%02d\n", 
		now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()) //YYYY-mm-dd HH:ii:ss
	//格式化以后返回给一个变量
	datestr := fmt.Sprintf("%02d-%02d-%02d %02d:%02d:%02d", 
			now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
	fmt.Println(datestr)
	
	//2006-01-02 15:04:05时间必须是这个时间 必须写这个时间 有点意思哈哈
	fmt.Println(now.Format("2006/01/02 15:04:05")) //YYYY/mm/dd HH:ii:ss
	fmt.Println(now.Format("2006/01/02")) //YYYY/mm/dd
	fmt.Println(now.Format("15")) //获取小时 其它同理

	i := 0
	for {
		i++
		fmt.Println(i)
		time.Sleep(time.Second) //休眠	time.Millisecond 毫秒
		if i >=10 {
			break
		}		
	}

	//获取时间戳,时间纳秒时间戳
	fmt.Printf("unix时间戳=%v,unixNano=%v", now.Unix(), now.UnixNano())
}
相关文章
相关标签/搜索