zoukankan      html  css  js  c++  java
  • Go语言学习笔记五--时间time的相关处理以及时间格式化

     1 package main
     2 
     3 import (
     4     "fmt"
     5     "time"
     6 )
     7 
     8 func main() {
     9 
    10     now := time.Now()
    11     fmt.Printf("
    current time is %v
    ",now)
    12 
    13     fmt.Printf("%d-%02d-%02d %02d:%02d:%02d
    ",now.Year(),now.Month(),now.Day(),now.Hour(),now.Month(),now.Second())
    14 
    15     //获取时间戳 从1970年到现在的秒数
    16     timeNum := now.Unix()
    17     println(timeNum)
    18     timeNum += 10000000
    19     time1 := time.Unix(timeNum,0)
    20     fmt.Printf("current time is %v
    ",time1)
    21 
    22     //定时器
    23     /*
    24     ticker  := time.Tick(time.Second)
    25     for i := range ticker{
    26         fmt.Printf("定时器时间 %v
    ",i)
    27         //这里可以放一个定时任务 比如上面就是每1秒钟打印输出一次
    28     }
    29     */
    30 
    31     //时间类里面的常量 在go语言中 大写默认表示公用变量可以访问,小写表示私有变量外部无法访问,函数方法亦是如此
    32     fmt.Printf("纳秒 %d
    ",time.Nanosecond)
    33     fmt.Printf("微秒 %d
    ",time.Microsecond)
    34     fmt.Printf("毫秒 %d
    ",time.Millisecond)
    35     fmt.Printf("秒 %d
    ",time.Second)
    36 
    37     //时间格式化
    38     //go语言的诞生时间 2006 01 02 15:04:05 时间格式化里面的参数只能填写这些数字要不然会格式化错误
    39     //真尼玛变态 恶心人 wcnm yyyy-mm-dd HH:MM:SS 他不香吗?
    40     timeStr := now.Format("2006/01/02 15:04:05")
    41     println(timeStr)
    42     timeStr = now.Format("2006-01-02 03:04:05")
    43     println(timeStr)
    44     timeStr = now.Format("2006**01**02 15/04/05")
    45     println(timeStr)
    46 
    47     //怎么求一个时间的耗时呢?(提示:可以利用时间戳Unix)
    48 }
  • 相关阅读:
    bzoj 1367
    codeforces 757F
    bzoj 3600
    比赛环境设置
    线段树合并
    BZOJ2105: 增强型LCP
    BZOJ3156: 防御准备
    BZOJ3252: 攻略
    BZOJ2464: 中山市选[2009]小明的游戏
    Beta Round #9 (酱油杯noi考后欢乐赛)乌鸦喝水
  • 原文地址:https://www.cnblogs.com/xwxz/p/13299351.html
Copyright © 2011-2022 走看看