Go Language - Get Current Date and Time

Hello everyone, today we are going to learn how to get the current date and time in Go Language. We need to import the “time” package in our Go script to work with date and time.

Go Program to find current Date and Time:

package main

import (
"fmt"
"time"
)

func main() {

dt := time.Now()

fmt.Println(dt.String())

//Formatted Date and Time

//Format MM/DD/YYYY
fmt.Println(dt.Format("01/02/2006"))

//Format MM-DD-YYYY
fmt.Println(dt.Format("01-02-2006"))

//Format MM.DD.YYYY
fmt.Println(dt.Format("01.02.2006"))

//Format MM-DD-YYYY hh:mm:ss
fmt.Println(dt.Format("01-02-2006 15:04:05"))

//With ShortWeek Day
fmt.Println(dt.Format("01-02-2006 Mon"))

//With weekday (Monday)
fmt.Println(dt.Format("01-02-2006 15:04:05 Monday"))

//Include micro seconds
fmt.Println(dt.Format("01-02-2006 15:04:05.000000"))

//Include nano seconds
fmt.Println(dt.Format("01/02/2006 15:04:05.000000000"))
}

Output:

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete