首页 > 其他分享 >Go - Formatting time

Go - Formatting time

时间:2023-10-06 13:12:41浏览次数:30  
标签:Format fmt 23 Println Formatting time Go Oct

 

func   main ()   { 
  t   :=   time . Now () 
   fmt . Println ( t . Format ( "3:04PM" )) 
   fmt . Println ( t . Format ( "Jan  02,  2006" )) 
}

When you run this you should see something like:

1:45PM
Oct  23,  2021

That’s simple enough. The time package makes it even simpler because it has several layout constants that you can use directly (note those RFCs described earlier):

func   main ()   { 
   t   :=   time . Now () 
   fmt . Println ( t . Format ( time . UnixDate )) 
   fmt . Println ( t . Format ( time . RFC822 )) 
   fmt . Println ( t . Format ( time . RFC850 )) 
   fmt . Println ( t . Format ( time . RFC1123 )) 
   fmt . Println ( t . Format ( time . RFC3339 )) 
}

Here’s the output:

Sat  Oct  23  15:05:37  +08  2021
22  Oct  23  15:05  +08
Saturday,  23 - Oct - 21  15:05:37  +08
Sat,  23  Oct  2021  15:05:37  +08
2021 - 10 - 23T15:05:37+08:00

Besides the RFCs, there are a few other formats including the interestingly named Kitchen layout, which is just 3:04 p.m. Also if you’re interested in doing timestamps, there are a few timestamp layouts as well:

func   main ()   { 
   t   :=   time . Now () 
   fmt . Println ( t . Format ( time . Stamp )) 
   fmt . Println ( t . Format ( time . StampMilli )) 
   fmt . Println ( t . Format ( time . StampMicro )) 
   fmt . Println ( t . Format ( time . StampNano )) 
}

Here’s the output:

Oct  23  15:10:53
Oct  23  15:10:53.899
Oct  23  15:10:53.899873
Oct  23  15:10:53.899873000

You might have seen earlier that the layout patterns are like this:

t . Format ( "3:04PM" )

The full layout pattern is a layout by itself — it’s the time.Layout constant:

const Layout = "01/02 03:04:05PM 06 - 0700"

As you can see, the numerals are ascending, starting with 1 and ending with 7. Because of a historic error (mentioned in the time package documentation), the date uses the American convention of putting the numerical month before the day. This means 01/02 is January 2 and not 1 February.

The numbers are not arbitrary. Take this code fragment, where you use the format “ 3:09pm ” instead of “ 3:04pm ”:

func   main ()   { 
   t   :=   time . Date ( 2009 ,   time . November ,   10 ,   23 ,   45 ,   0 ,   0 ,   time . UTC ) 
   fmt . Println ( t . Format ( time . Kitchen )) 
   fmt . Println ( t . Format ( "3:04pm" ))   //  the  correct  layout 
   fmt . Println ( t . Format ( "3:09pm" ))   //  mucking  around 
}

This is the output:

11:45pm
11:45pm
11:09pm

You can see that the time is 11:45 p.m., but when you use the layout 3:09 p.m., the hour is displayed correctly while the minute is not. It shows :09, which means it’s considering 09 as the label instead of a layout for minute.

What this means is that the numerals are not just a placeholder for show. The month must be 1, day must be 2, hour must be 3, minute must be 4, second must be 5, year must be 6, and the time zone must be 7.

 

标签:Format,fmt,23,Println,Formatting,time,Go,Oct
From: https://www.cnblogs.com/zhangzhihui/p/17744464.html

相关文章

  • Go - Measuring Lapsed Time
    Problem: Youwanttomeasurethelapsedtimeandmakesurethatitisaccurate.Solution: UsethemonotonicclockintheTimestructtofindthelapsedtime. TheTimestructcontainsthedatabutonlyundercertaincircumstances.IfyoucreateaTimes......
  • go通过pprof定位groutine泄漏
    日常开发中除了会出现Panic、ErrorInfo等通过日志很容易捕捉到的错误,还会出现内存泄漏、CPU激增等日志难以捕捉的问题。今天小老虎就给大家介绍下如何使用pprof去捕捉内存、CPU这些日志难以排查到的问题。pprof的访问pprof是Golang的性能分析工具,可以帮助我们查看程序在运行过程中C......
  • 基于Django的智慧旅游系统的设计与实现-计算机毕业设计源码+LW文档
    摘 要在各学校的教学过程中,智慧旅游系统是一项非常重要的事情。随着计算机多媒体技术的发展和网络的普及。采用当前流行的B/S模式以及3层架构的设计思想通过Python技术来开发此系统的目的是建立一个配合网络环境的智慧旅游系统,这样可以有效地解决智慧旅游管理信息混乱的局面。......
  • go语言ent教程:使用zerolog定制ent日志
    背景:ent开始debug模式后,可以输出日志,但是我们想为ent接入zerolog,该怎么做呢? 一、引入zerologgoget-ugithub.com/rs/zerolog 二、自定义zerolog配置customLog:=func(args...any){str:=fmt.Sprintf("%v",args)fmt.Println(str)......
  • go语言ent教程:开启debug调试模式
    背景:ent模式是没有开启debug模式的 开启ent的调试模式有2种方法:一、通过配置选项开启client,err:=ent.Open("mysql","root:dev@123456@tcp(localhost:3306)/test?parseTime=True",ent.Debug()) 二、客户端调用Debug函数iferr:=client.Schema.Crea......
  • 《制作Docker镜像》——以Django镜像为例
    一、建一个基础的Ubuntu环境执行以下指令,docker就会搞一个ubuntu环境,如果你没有嘞?它就会自己下载的。dockerrun-itubtuntu然后你就会发现,自己进入了镜像了。二、给容器换源那么这个我们就只需要去清华的镜像。然后选择自己对应的版本。【--->Ubuntu软件仓库<---】接......
  • Go - Representing Duration
    Problem: Youwanttospecifyadurationoftime.Solution: UsetheDurationtypetorepresentaspanoftime. Themainrepresentationforaspanoftimeinthetimepackageis,ofcourse,Duration.Durationisnothingfancy,though;it’sjustanint64......
  • 将np.datetime64的时间差转为秒
    print((np.datetime64('2023-01-0200:00:00')-np.datetime64('2023-01-0100:00:00'))/np.timedelta64(1,'s'),(pd.Series(np.datetime64('2023-01-0200:00:00'))-pd.Series(np.datetime64('2023-01-0100:0......
  • C++ Profiler Introduction [CPU Time Only]
    C++ProfilerIntroduction[CPUTimeOnly]author:LastWhisperdate:2023/10/05ThereareseveralprofilersforC++.Basedonmyresearch,I'vefoundthattracyisthemostpowerful.However,it'schallengingtoconfigure.Toquicklybenchmark......
  • MongoDB高阶特性:事务、索引
    一、事务一)MongoDB的事务首先我们需要知道MongoDB是有多种存储引擎的,不同的存储引擎在实现ACID的时候,使用不同的机制。而Mongodb从3.0开始默认使用的是WiredTiger引擎,本文后续所有文字均是针对WiredTiger引擎。WiredTiger引擎可以针对单个文档来保证ACID特性,但是当需要操作多个......