How can I get the date without the time part

I know I can use something line below to format the time the way I want:

// Golang program to get the current 
// date and time in various format 
package main 
  
import ( 
    "fmt"
    "time"
) 
  
func main() { 
  
    // using time.Now() function 
    // to get the current time 
    currentTime := time.Now() 
  
    // getting the time in string format 
    fmt.Println("Show Current Time in String: ", currentTime.String()) 
  
    fmt.Println("YYYY.MM.DD : ", currentTime.Format("2017.09.07 17:06:06")) 
  
    fmt.Println("YYYY#MM#DD {Special Character} : ", currentTime.Format("2017#09#07")) 
  
    fmt.Println("MM-DD-YYYY : ", currentTime.Format("09-07-2017")) 
  
    fmt.Println("YYYY-MM-DD : ", currentTime.Format("2017-09-07")) 
  
    fmt.Println("YYYY-MM-DD hh:mm:ss : ", currentTime.Format("2017-09-07 17:06:06")) 
  
    fmt.Println("Time with MicroSeconds: ", currentTime.Format("2017-09-07 17:06:04.000000")) 
  
    fmt.Println("Time with NanoSeconds: ", currentTime.Format("2017-09-07 17:06:04.000000000")) 
  
    fmt.Println("ShortNum Wedth : ", currentTime.Format("2017-02-07")) 
  
    fmt.Println("ShortYear : ", currentTime.Format("06-Feb-07")) 
  
    fmt.Println("LongWeekDay : ", currentTime.Format("2017-09-07 17:06:06 Wednesday")) 
  
    fmt.Println("ShortWeek Day : ", currentTime.Format("2017-09-07 Wed")) 
  
    fmt.Println("ShortDay : ", currentTime.Format("Wed 2017-09-2")) 
  
    fmt.Println("LongWedth : ", currentTime.Format("2017-March-07")) 
  
    fmt.Println("ShortWedth : ", currentTime.Format("2017-Feb-07")) 
  
    fmt.Println("Short Hour Minute Second: ", currentTime.Format("2017-09-07 2:3:5 PM")) 
  
    fmt.Println("Short Hour Minute Second: ", currentTime.Format("2017-09-07 2:3:5 pm")) 
  
    fmt.Println("Short Hour Minute Second: ", currentTime.Format("2017-09-07 2:3:5")) 
  
} 

By this is returning a string not a date, so if I used:

d := currentTime.Format("06-Feb-07")

Then d is a String, but I need to have d as a date?

Date is a struct. You get string when you format it. May be, you need beginning of the specified day? Where do you want to use it ?

I do not know if this helps you, but I asked about the same question some time ago

1 Like

The date without the time part:

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()
	fmt.Println(now.Round(0))
	y, m, d := now.Date()
	fmt.Println(y, m, d)
	fmt.Println(y, int(m), d)
}

https://play.golang.org/p/cYO8KV4jLoQ

2009-11-10 23:00:00 +0000 UTC
2009 November 10
2009 11 10
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.