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

Spring boot video streaming example-HTML5

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Custom Exception Handling in Quarkus REST API

Registration and Login with Spring Boot + Spring Security + Thymeleaf

DataTable-Pagination example with Spring boot, jQuery and ajax

Spring Webflux File Download - REST API Example

Node JS mini projects with source code - free download

ReactJS, Spring Boot JWT Authentication Example