-3

Is it possible to store the timestamp in a variable like a log function when printing out? Because time.Now is giving too much detail. I did try something like:

    //dt := time.Now()
    //times := dt.String()
    x := []string{cokeAmountStr, spriteAmountStr, fantaAmountStr, mirindaAmountStr, mineralWaterAmountStr, priceStr, mReceivedStr, mReturnStr}
    transaction = (strings.Join(x, ","))
    var data string = log.Println(transaction)
}

But it won't work

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • 1
    [time.Time](https://golang.org/pkg/time/#Time) has a number of methods on it for different ways to format it into a string or an integer. Read through the docs and see which one works for you. – Marc Sep 15 '20 at 07:33
  • 1
    What does "it didn't work" mean? What problem did you encounter? – Jonathan Hall Sep 15 '20 at 07:49
  • 1
    I also don't understand what you mean at all. `dt := time.Now()` *does* store a timestamp in a variable. What else do you want? – Jonathan Hall Sep 15 '20 at 07:50

1 Answers1

1

Hi and welcome to StackOverflow.

to get Unix time you can use time.Unix():

var timestamp int64
timestamp = time.Now().Unix()

also, you can get formatted string from time

var formatted string
t := time.now()
layout := "2006-01-02T15:04:05Z07:00"
t.Format(layout)

list of layout constats : https://golang.org/pkg/time/#pkg-constants

ttrasn
  • 4,322
  • 4
  • 26
  • 43
  • If this is the answer to the question, then the question is a duplicate of [this](https://stackoverflow.com/q/9539108/13860), and probably a dozen others. – Jonathan Hall Sep 15 '20 at 07:53
  • @Flimzy Yes, I think this is the answer to this question, cause he said `time.Now is giving too much detail`, also I will add `format` to my answer. – ttrasn Sep 15 '20 at 08:00
  • 1
    `time.Format` is also well covered in other Q&As. Whatever the answer, I'm about 99.99% positive that this question is a duplicate. But since the question is completely unclear, it's hard to say what it's a duplicate of. It's usually best to get the OP to clarify the question, rather than providing random answers that may or may not be on target. – Jonathan Hall Sep 15 '20 at 08:01
  • @Flimzy Yep, you are right. so should I delete my answer? – ttrasn Sep 15 '20 at 08:06
  • 1
    @JackNicolus_XI: That's a unix timestamp and is a standard way to represent time. If this doesn't work for you, clarify your question, we don't know what you're looking for. – Marc Sep 15 '20 at 08:30
  • What I would like to have is a simple date and time for the time stamp. But time.Now() is too much detailed. – JackNicolus_XI Sep 15 '20 at 08:37
  • Now I'm trying to use .Format("2017.09.07 17:06:06") and I'm having 15097.09.07 97:20:20 as a result. – JackNicolus_XI Sep 15 '20 at 08:44
  • Use the [Format](https://golang.org/pkg/time/#Time.Format) method to pass in a custom format, or use one of the one of the [predefined-ones](https://golang.org/pkg/time/#pkg-constants). It's all in the docs. – Marc Sep 15 '20 at 08:44
  • 1
    This is not how the format is specified, it needs to match the reference date shown in the docs. In your case: `2006.01.02 15:04:05`. – Marc Sep 15 '20 at 08:46