Go时间格式化的两种方法

栏目: Go · 发布时间: 6年前

内容简介:时间格式在程序、数据库、日志信息中扮演着非常重要的角色,选择合适的格式能为我们的工作带来编辑。在Go语言中可以使用time包实现我们基础的需求。导入包时间输出以及格式化

时间格式在程序、数据库、日志信息中扮演着非常重要的角色,选择合适的格式能为我们的工作带来编辑。在 Go 语言中可以使用time包实现我们基础的需求。

导入包

import (
   "time"
   "fmt"
)

时间输出以及格式化

package main

import (
   "time"
   "fmt"
)

//时间函数以及时间格式化

func  main(){
   now  := time.Now()
   //Year = now.Year()
   //Mouth  = now.Month()
   //Day  =  now.Day()
   //时间格式化输出 Printf输出
   fmt.Printf("当前时间为: %d-%d-%d %d:%d:%d\n",now.Year(),now.Month(),now.Day(),now.Hour(),now.Minute(),now.Second())
//fmt.Sprintf 格式化输出
  dateString := fmt.Sprintf("当前时间为: %d-%d-%d %d:%d:%d\n",now.Year(),now.Month(),now.Day(),now.Hour(),now.Minute(),now.Second())
   fmt.Println(dateString)
 //now.Format 方法格式化
  fmt.Println(now.Format("2006-01-02 15:04:05"))
  fmt.Println(now.Format("2006/01/02 15:04:05"))
  fmt.Println(now.Format("2006/01/02"))//年月日
  fmt.Println(now.Format("15:04:05"))//时分秒


   }

输出结果

Go时间格式化的两种方法

注意事项:用now.Format()方式 其字符串的格式是特定的"2006-01-02 15:04:05",我们在使用的时候可以根据需求对其更改,例如只显示年月日,以及时间的间隔符号等。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Programming Concurrency on the JVM

Programming Concurrency on the JVM

Venkat Subramaniam / The Pragmatic Bookshelf / 2011-6-1 / USD 35.00

Concurrency on the Java platform has evolved, from the synchronization model of JDK to software transactional memory (STM) and actor-based concurrency. This book is the first to show you all these con......一起来看看 《Programming Concurrency on the JVM》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

html转js在线工具
html转js在线工具

html转js在线工具