golang时间函数

    技术2022-07-16  76

    //【时间获取及格式化】 //获取当前时间 now_time := time.Now() fmt.Printf("Now_time=%v,数据类型:%T \n", now_time, now_time) //通过now获取年月日时分秒 fmt.Printf("年=%v\n", now_time.Year()) fmt.Printf("月=%v\n", now_time.Month()) //默认是英文的月份 fmt.Printf("月=%v\n", int(now_time.Month())) //加int转换为数字 fmt.Printf("日=%v\n", now_time.Day()) fmt.Printf("时=%v\n", now_time.Hour()) fmt.Printf("分=%v\n", now_time.Minute()) fmt.Printf("秒=%v\n", now_time.Second()) //格式化日期时间① fmt.Printf("当前时间 %d-%d-%d %d:%d:%d \n", now_time.Year(), int(now_time.Month()), now_time.Day(), now_time.Hour(), now_time.Minute(), now_time.Second()) //把格式化好的时间返回给一个变量,然后输出 date_now := fmt.Sprintf("当前时间 %d-%d-%d %d:%d:%d \n", now_time.Year(), int(now_time.Month()), now_time.Day(), now_time.Hour(), now_time.Minute(), now_time.Second()) fmt.Printf("date:%v\n", date_now) //格式化日期时间② //2006/01/02 15:04:05 这里必须数字一个不差的写 //据说是因为golang设计者在这个时间有设计golang的想法 fmt.Printf(now_time.Format("2006/01/02 15:04:05\n")) fmt.Printf(now_time.Format("2006/01/02\n")) fmt.Printf(now_time.Format("15:04:05\n")) //【时间常量应用】 // 时间单位换算 // const { // Nanosecond Duration = 1 //纳秒 // Microsecond = 1000 * Nanosecond //微秒 // Millisecond = 1000 * Microsecond = 1000000(1000 * 1000) * Nanosecond //毫秒 // Second = 1000 * Millisecond = 1000000(1000 * 1000) * Microsecond = 1000000000(1000 * 1000 * 1000) * Nanosecond //秒 // Minute = 60 * Second //分钟 // Hour = 60 * Minute //小时 // } time.Sleep(time.Second) // 每隔1秒 time.Sleep(time.Millisecond * 100) // 每隔0.1秒
    Processed: 0.009, SQL: 9