mirror of
https://github.com/jbranchaud/til
synced 2026-01-20 23:48:02 +00:00
Compare commits
4 Commits
1ac4a41163
...
3af19869c3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3af19869c3 | ||
|
|
f5286c1f41 | ||
|
|
5615da920f | ||
|
|
c60c63f554 |
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1537 TILs and counting..._
|
_1538 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -191,7 +191,7 @@ _1537 TILs and counting..._
|
|||||||
- [Aliasing An Ansible Host](devops/aliasing-an-ansible-host.md)
|
- [Aliasing An Ansible Host](devops/aliasing-an-ansible-host.md)
|
||||||
- [Allow Cross-Origin Requests To Include Cookies](devops/allow-cross-origin-requests-to-include-cookies.md)
|
- [Allow Cross-Origin Requests To Include Cookies](devops/allow-cross-origin-requests-to-include-cookies.md)
|
||||||
- [Allow HTTPS Through Your UFW Firewall](devops/allow-https-through-your-ufw-firewall.md)
|
- [Allow HTTPS Through Your UFW Firewall](devops/allow-https-through-your-ufw-firewall.md)
|
||||||
- [Check For Cached Site Assocation File For iOS](devops/check-for-cached-site-association-file-for-ios.md)
|
- [Check For Cached Site Association File For iOS](devops/check-for-cached-site-association-file-for-ios.md)
|
||||||
- [Check The Status of All Services](devops/check-the-status-of-all-services.md)
|
- [Check The Status of All Services](devops/check-the-status-of-all-services.md)
|
||||||
- [Check The Syntax Of nginx Files](devops/check-the-syntax-of-nginx-files.md)
|
- [Check The Syntax Of nginx Files](devops/check-the-syntax-of-nginx-files.md)
|
||||||
- [Connect To An RDS PostgreSQL Database](devops/connect-to-an-rds-postgresql-database.md)
|
- [Connect To An RDS PostgreSQL Database](devops/connect-to-an-rds-postgresql-database.md)
|
||||||
@@ -405,6 +405,7 @@ _1537 TILs and counting..._
|
|||||||
- [Combine Two Slices](go/combine-two-slices.md)
|
- [Combine Two Slices](go/combine-two-slices.md)
|
||||||
- [Do Something N Times](go/do-something-n-times.md)
|
- [Do Something N Times](go/do-something-n-times.md)
|
||||||
- [Find Executables Installed By Go](go/find-executables-installed-by-go.md)
|
- [Find Executables Installed By Go](go/find-executables-installed-by-go.md)
|
||||||
|
- [Format Date And Time With Time Constants](go/format-date-and-time-with-time-constants.md)
|
||||||
- [Not So Random](go/not-so-random.md)
|
- [Not So Random](go/not-so-random.md)
|
||||||
- [Parse A String Into Individual Fields](go/parse-a-string-into-individual-fields.md)
|
- [Parse A String Into Individual Fields](go/parse-a-string-into-individual-fields.md)
|
||||||
- [Parse Flags From CLI Arguments](go/parse-flags-from-cli-arguments.md)
|
- [Parse Flags From CLI Arguments](go/parse-flags-from-cli-arguments.md)
|
||||||
@@ -741,7 +742,7 @@ _1537 TILs and counting..._
|
|||||||
- [Check If Clusters Are Upgrade Compatible](postgres/check-if-clusters-are-upgrade-compatible.md)
|
- [Check If Clusters Are Upgrade Compatible](postgres/check-if-clusters-are-upgrade-compatible.md)
|
||||||
- [Check If The Local Server Is Running](postgres/check-if-the-local-server-is-running.md)
|
- [Check If The Local Server Is Running](postgres/check-if-the-local-server-is-running.md)
|
||||||
- [Check If User Role Exists For Database](postgres/check-if-user-role-exists-for-database.md)
|
- [Check If User Role Exists For Database](postgres/check-if-user-role-exists-for-database.md)
|
||||||
- [Check Table For Any Oprhaned Records](postgres/check-table-for-any-orphaned-records.md)
|
- [Check Table For Any Orphaned Records](postgres/check-table-for-any-orphaned-records.md)
|
||||||
- [Checking Inequality](postgres/checking-inequality.md)
|
- [Checking Inequality](postgres/checking-inequality.md)
|
||||||
- [Checking The Type Of A Value](postgres/checking-the-type-of-a-value.md)
|
- [Checking The Type Of A Value](postgres/checking-the-type-of-a-value.md)
|
||||||
- [Clear The Screen In psql](postgres/clear-the-screen-in-psql.md)
|
- [Clear The Screen In psql](postgres/clear-the-screen-in-psql.md)
|
||||||
|
|||||||
51
go/format-date-and-time-with-time-constants.md
Normal file
51
go/format-date-and-time-with-time-constants.md
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
# Format Date And Time With Time Constants
|
||||||
|
|
||||||
|
The Go [`time` package](https://pkg.go.dev/time) has a [`Format`
|
||||||
|
function](https://pkg.go.dev/time#Time.Format) for displaying the parts of a
|
||||||
|
date and time in standard and custom ways. It works a bit different than you
|
||||||
|
might be used to from other languages. Rather than using `strftime` identifiers
|
||||||
|
like in this string `"%B %d, %Y"`, there is a canonical date that is used as a
|
||||||
|
reference point.
|
||||||
|
|
||||||
|
That canonical date is from Janary 2nd, 2006. That was a Monday. It was at 5
|
||||||
|
seconds after 3:04PM. The Unix format of it looks like `"Mon Jan _2 15:04:05
|
||||||
|
MST 2006"`.
|
||||||
|
|
||||||
|
```
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// This specific time pulled from `time.Format` docs
|
||||||
|
t, _ := time.Parse(time.UnixDate, "Wed Feb 25 11:06:39 PST 2015")
|
||||||
|
|
||||||
|
// Reference date and time:
|
||||||
|
// "Mon Jan _2 15:04:05 MST 2006"
|
||||||
|
|
||||||
|
strf1 := t.Format("|2006|02|01|03:04:05|Day: Mon|")
|
||||||
|
fmt.Println("strf1:", strf1)
|
||||||
|
// strf1: |2015|25|02|11:06:39|Day: Wed|
|
||||||
|
|
||||||
|
strf2 := t.Format(time.DateTime)
|
||||||
|
strf3 := t.Format(time.RubyDate)
|
||||||
|
strf4 := t.Format(time.Kitchen)
|
||||||
|
|
||||||
|
fmt.Println("DateTime:", strf2) // DateTime: 2015-02-25 11:06:39
|
||||||
|
fmt.Println("RubyDate:", strf3) // RubyDate: Wed Feb 25 11:06:39 +0000 2015
|
||||||
|
fmt.Println("Kitchen:", strf4) // Kitchen: 11:06AM
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Though there are a [variety of useful formatting
|
||||||
|
constants](https://pkg.go.dev/time#pkg-constants) already available like
|
||||||
|
`DateTime`, `RubyDate`, `Kitchen`, etc., we can also define our own formatting
|
||||||
|
string by using the reference values for each part of a date and time.
|
||||||
|
|
||||||
|
If you want to reference the year, whether as `YYYY` or `YY`, it is always
|
||||||
|
going to be a form of `2006`, so `2006` or `06` respectively. Even though the
|
||||||
|
above time variable is in February, our format strings will always need to use
|
||||||
|
one of `Jan`, `January`, `01` or `1`.
|
||||||
Reference in New Issue
Block a user