{"id":1225,"date":"2020-01-25T17:48:30","date_gmt":"2020-01-25T17:48:30","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=1225"},"modified":"2020-01-25T17:48:36","modified_gmt":"2020-01-25T17:48:36","slug":"time-date-formatting-in-go","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/time-date-formatting-in-go\/","title":{"rendered":"Time\/Date Formatting in Go"},"content":{"rendered":"\n
If you have worked with time\/date formatting in other languages you might have noticed that the other languages use special placeholders for time\/date formatting. For eg ruby language uses<\/p>\n\n\n\n
etc<\/p>\n\n\n\n
In Golang date and time format placeholders look like date and time only. Refer to below placeholder table<\/p>\n\n\n\n It is easy to remember the above placeholders when represented in sequence. <\/p>\n\n\n\n Weekday-Month-Day-Hour-Min-Seconds-Year-Timezone <\/strong>will be Mon-01-02-03-04-05-06–07<\/strong><\/p>\n\n\n\n Let’s see some time format code examples<\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n If you have worked with time\/date formatting in other languages you might have noticed that the other languages use special placeholders for time\/date formatting. For eg ruby language uses %d for day…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[4,175],"class_list":["post-1225","post","type-post","status-publish","format-standard","hentry","category-tech","tag-golang","tag-time-package"],"yoast_head":"\nType<\/strong><\/td> Placeholder<\/strong><\/td><\/tr> Day<\/td> 2<\/strong> or 02<\/strong> or _2<\/strong><\/td><\/tr> Day of Week<\/td> Monday<\/strong> or Mon<\/strong><\/td><\/tr> Month<\/td> 01<\/strong> or 1<\/strong> or Jan<\/strong> or January<\/strong><\/td><\/tr> Year<\/td> 2006<\/strong> or 06<\/strong><\/td><\/tr> Hour<\/td> 03<\/strong> or 3 <\/strong>or 15<\/strong><\/td><\/tr> Minutes<\/td> 04<\/strong> or 4<\/strong><\/td><\/tr> Seconds<\/td> 05<\/strong> or 5<\/strong><\/td><\/tr> MilliSeconds\u00a0 (ms)<\/td> .000\u00a0 \u00a0 \u00a0 \u00a0 <\/strong>\/\/Trailing zero will be included
or .999 <\/strong>\u00a0 \/\/Trailing zero will be omitted<\/td><\/tr>Micro Seconds (\u03bcs)<\/td> .000000\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 <\/strong>\/\/Trailing zero will be included
or .999999<\/strong>\u00a0 \u00a0 \u00a0 \u00a0 \/\/Trailing zero will be omitted<\/td><\/tr>Nano Seconds (ns)<\/td> .000000000\u00a0 \u00a0 \u00a0 \u00a0 <\/strong>\/\/Trailing zero will be included
or .999999999 <\/strong> \/\/Trailing zero will be omitted<\/td><\/tr>am\/pm<\/td> PM<\/strong> or pm<\/strong><\/td><\/tr> Timezone<\/td> MST<\/strong><\/td><\/tr> Timezone offset<\/td> Z0700 <\/strong>or Z070000<\/strong> or Z07<\/strong> or Z07:00<\/strong> or Z07:00:00<\/strong> or -0700<\/strong> or -070000<\/strong> or -07<\/strong> or -07:00<\/strong> or -07:00:00<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n package main\n\nimport (\n \"fmt\"\n \"time\"\n)\n\nfunc main() {\n now := time.Now()\n \n \/\/Format YYYY-MM-DD\n fmt.Printf(\"YYYY-MM-DD: %s\\n\", now.Format(\"2006-01-02\"))\n\n \/\/Format YY-MM-DD\n fmt.Printf(\"YY-MM-DD: %s\\n\", now.Format(\"06-01-02\"))\n\n \/\/Format YYYY-#{MonthName}-DD\n fmt.Printf(\"YYYY-#{MonthName}-DD: %s\\n\", now.Format(\"2006-Jan-02\"))\n\n \/\/Format HH:MM:SS\n fmt.Printf(\"HH:MM:SS: %s\\n\", now.Format(\"03:04:05\"))\n\n \/\/Format HH:MM:SS Millisecond\n fmt.Printf(\"HH:MM:SS Millisecond: %s\\n\", now.Format(\"03:04:05 .999\"))\n\n \/\/Format YYYY-#{MonthName}-DD WeekDay HH:MM:SS\n fmt.Printf(\"YYYY-#{MonthName}-DD WeekDay HH:MM:SS: %s\\n\", now.Format(\"2006-Jan-02 Monday 03:04:05\"))\n\n \/\/Format YYYY-#{MonthName}-DD WeekDay HH:MM:SS PM Timezone TimezoneOffset\n fmt.Printf(\"YYYY-#{MonthName}-DD WeekDay HH:MM:SS PM Timezone TimezoneOffset: %s\\n\", now.Format(\"2006-Jan-02 Monday 03:04:05 PM MST -07:00\"))\n}<\/code><\/pre>\n\n\n\n
YYYY-MM-DD: 2020-01-25\nYY-MM-DD: 20-01-25\nYYYY-#{MonthName}-DD: 2020-Jan-25\nHH:MM:SS: 11:14:16\nHH:MM:SS Millisecond: 11:14:16 .213\nYYYY-#{MonthName}-DD WeekDay HH:MM:SS: 2020-Jan-25 Saturday 11:14:16\nYYYY-#{MonthName}-DD WeekDay HH:MM:SS PM Timezone TimezoneOffset: 2020-Jan-25 Saturday 11:14:16 PM IST +05:30<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"