Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to convert DateTime to string in Go

1 Answer

0 votes
package main

import (
	"fmt"
	"time"
)

func main() {
	dt := time.Now()

	dt_str := dt.Format("2006-01-02 15:04:05")

	fmt.Printf("%v type is: %T \n", dt_str, dt_str)
}



/*
run:

2024-08-12 09:04:32 type is: string

*/


/*
// Go uses constants to format date:

const (

	_                        = iota

	stdLongMonth             = iota + stdNeedDate  // "January"

	stdMonth                                       // "Jan"

	stdNumMonth                                    // "1"

	stdZeroMonth                                   // "01"

	stdLongWeekDay                                 // "Monday"

	stdWeekDay                                     // "Mon"

	stdDay                                         // "2"

	stdUnderDay                                    // "_2"

	stdZeroDay                                     // "02"

	stdUnderYearDay                                // "__2"

	stdZeroYearDay                                 // "002"

	stdHour                  = iota + stdNeedClock // "15"

	stdHour12                                      // "3"

	stdZeroHour12                                  // "03"

	stdMinute                                      // "4"

	stdZeroMinute                                  // "04"

	stdSecond                                      // "5"

	stdZeroSecond                                  // "05"

	stdLongYear              = iota + stdNeedDate  // "2006"

	stdYear                                        // "06"

	stdPM                    = iota + stdNeedClock // "PM"

	stdpm                                          // "pm"

	stdTZ                    = iota                // "MST"

	stdISO8601TZ                                   // "Z0700"  // prints Z for UTC

	stdISO8601SecondsTZ                            // "Z070000"

	stdISO8601ShortTZ                              // "Z07"

	stdISO8601ColonTZ                              // "Z07:00" // prints Z for UTC

	stdISO8601ColonSecondsTZ                       // "Z07:00:00"

	stdNumTZ                                       // "-0700"  // always numeric

	stdNumSecondsTz                                // "-070000"

	stdNumShortTZ                                  // "-07"    // always numeric

	stdNumColonTZ                                  // "-07:00" // always numeric

	stdNumColonSecondsTZ                           // "-07:00:00"

	stdFracSecond0                                 // ".0", ".00", ... , trailing zeros included

	stdFracSecond9                                 // ".9", ".99", ..., trailing zeros omitted


	stdNeedDate       = 1 << 8             // need month, day, year

	stdNeedClock      = 2 << 8             // need hour, minute, second

	stdArgShift       = 16                 // extra argument in high bits, above low stdArgShift

	stdSeparatorShift = 28                 // extra argument in high 4 bits for fractional second separators

	stdMask           = 1<<stdArgShift - 1 // mask out argument

)
*/

 



answered Aug 12, 2024 by avibootz
edited Aug 12, 2024 by avibootz
...