zoukankan      html  css  js  c++  java
  • time包中Parse和Format的区别

    time包中Parse和Format的区别

    • 参考代码
    package main
    
    import(
    	"fmt"
    	"time"
    )
    
    // 规定parse和Format的参考时间,这个是官方规定的
    const longForm = "Jan 2, 2006 at 3:04pm (MST)"
    const shortForm = "2006-Jan-02" 
    const timeFormat = "2006-01-02 15:04:05"
    
    func main(){
    	//根据格式,将字符串转变为时间类型,第一个参数表示时间的格式,第二参数表示目标字符串
    	//t, err:= time.Parse(longForm, time.Now().String())   //这个是错误,因为没有符合longForm的格式
    	//if err!=nil{
    	//	panic(err)
    	//}else{
    	//	fmt.Println(t)
    	//}
        t, _ := time.Parse(shortForm, "2020-Feb-03") 
    	fmt.Printf("类型: %T,值%v 
    ",t,t)
    	t2 := time.Now()
    	strTime := t2.Format(timeFormat)
    	fmt.Printf("类型: %T,值%v 
    ",strTime,strTime)
    }
    
    • 输出结果
    类型: time.Time,值2020-02-03 00:00:00 +0000 UTC
    类型: string,值2019-09-26 00:08:31
    
    • 总结:两者的区别:Format表示将时间转化为字符串,parse表示将字符串转化为时间
  • 相关阅读:
    三个Bootstrap免费字体和图标库
    前端实时消息提示的效果-websocket长轮询
    带分页的标签
    VMware-workstation安装
    摄影/肥猫的大头贴
    Smith Numbers(分解质因数)
    C
    B
    Ball
    Property Distribution(DFS)
  • 原文地址:https://www.cnblogs.com/MyUniverse/p/11588494.html
Copyright © 2011-2022 走看看