FAQ
Surely this is a common request?
Just looking for a quick and dirty solution.
Does anyone have a function or the code to do this?

==========================================
----Server Code (Golang)-----

d := time.Now()

//x := <<<<<This is the code I need>>>>

j, err := json.Marshal(x)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(j)

------Client Code (JavaScript)------
//....Ajax..... retrieve j

myDate = JSON.parse(j)

d = new Date(myDate)

==========================================

Thanks in advance to anyone who can provide the simplest way to do this
without new imports,
visits to github, or a lesson in date formatting (already been to the
dentist this month).

-G

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Ain at May 21, 2016 at 3:45 pm

    On Saturday, May 21, 2016 at 6:28:05 PM UTC+3, Gregg Murray wrote:
    Surely this is a common request?
    Just looking for a quick and dirty solution.
    Does anyone have a function or the code to do this?

    ==========================================
    ----Server Code (Golang)-----

    d := time.Now()

    //x := <<<<<This is the code I need>>>>
    I guess you want something along the lines

    type DateType struct{ time.Time }

    func (this *DateType) MarshalText() (result []byte, err error) {
         fmted := this.Format("2006-01-02") // here use the format required by
    JS parser
         return []byte(fmted), nil
    }

    type SendTOJS struct {
        D DateType `json:"D"`
    }

    var x SendTOJS
    x.D = time.Now()


    j, err := json.Marshal(x)
    if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
    }
    w.Write(j)
    HTH
    ain

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Frits van Bommel at May 21, 2016 at 3:55 pm
    Well, Javascript accepts the number of milliseconds since the Unix epoch as
    one of the options, so

    x := d.UnixNano() / int64(time.Millisecond / time.Nanosecond) //
    time.Nanosecond == 1, so dividing by it is redundant but more clearly
    correct.

    ?
    If you need to support dates over 2 centuries in the future (or just want a
    readable string instead of an opaque number) the Mozilla documentation at
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
    allows a format that looks suspiciously like time.RFC1123Z (though they
    call it something different), so

    x := d.Format(time.RFC1123Z)

    would probably work as well.

    On Saturday, May 21, 2016 at 5:28:05 PM UTC+2, Gregg Murray wrote:

    Surely this is a common request?
    Just looking for a quick and dirty solution.
    Does anyone have a function or the code to do this?

    ==========================================
    ----Server Code (Golang)-----

    d := time.Now()

    //x := <<<<<This is the code I need>>>>

    j, err := json.Marshal(x)
    if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
    }
    w.Write(j)

    ------Client Code (JavaScript)------
    //....Ajax..... retrieve j

    myDate = JSON.parse(j)

    d = new Date(myDate)

    ==========================================

    Thanks in advance to anyone who can provide the simplest way to do this
    without new imports,
    visits to github, or a lesson in date formatting (already been to the
    dentist this month).

    -G
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Gregg Murray at May 21, 2016 at 4:26 pm
    Thank you! Last question, will that method allow for java script to
    display the time as local to the browser user? This is a multi-time zone
    app. IE: Does time.Now().UnixNano() give local server time or UTC?


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Frits van Bommel at May 21, 2016 at 5:26 pm
    Unix time is specified as time since 1 jan 1970 UTC (by default in seconds
    but javascript uses milliseconds instead), which is also what the
    javascript Date object expects to get if you pass it a number.
    If you use my second suggestion of d.Format(time.RFC1123Z), that includes a
    timezone but I'm not sure if javascript will keep track of that or simply
    convert to whatever time zone it uses internally.

    Either way, getting local time out of it is then purely a javascript
    matter, so I'd look here
    <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date>
    and experiment a little (for instance, the toString() docs on that site
    don't specify which locale it uses, but given that there's also a
    toUTCString() I'd say the odds were good it uses local time).
    However, this isn't really the right place for javascript questions so if
    you need help with that I'd suggest taking the question(s) to a more
    appropriate group.


    On Saturday, May 21, 2016 at 6:26:01 PM UTC+2, Gregg Murray wrote:

    Thank you! Last question, will that method allow for java script to
    display the time as local to the browser user? This is a multi-time zone
    app. IE: Does time.Now().UnixNano() give local server time or UTC?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • C Banning at May 21, 2016 at 9:51 pm
    You're probably looking for something like:

    x := map[string]interface{}{

    On Saturday, May 21, 2016 at 10:28:05 AM UTC-5, Gregg Murray wrote:

    Surely this is a common request?
    Just looking for a quick and dirty solution.
    Does anyone have a function or the code to do this?

    ==========================================
    ----Server Code (Golang)-----

    d := time.Now()

    //x := <<<<<This is the code I need>>>>

    j, err := json.Marshal(x)
    if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
    }
    w.Write(j)

    ------Client Code (JavaScript)------
    //....Ajax..... retrieve j

    myDate = JSON.parse(j)

    d = new Date(myDate)

    ==========================================

    Thanks in advance to anyone who can provide the simplest way to do this
    without new imports,
    visits to github, or a lesson in date formatting (already been to the
    dentist this month).

    -G
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • C Banning at May 21, 2016 at 9:55 pm
    https://play.golang.org/p/erMKygqA_1
    On Saturday, May 21, 2016 at 10:28:05 AM UTC-5, Gregg Murray wrote:

    Surely this is a common request?
    Just looking for a quick and dirty solution.
    Does anyone have a function or the code to do this?

    ==========================================
    ----Server Code (Golang)-----

    d := time.Now()

    //x := <<<<<This is the code I need>>>>

    j, err := json.Marshal(x)
    if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
    }
    w.Write(j)

    ------Client Code (JavaScript)------
    //....Ajax..... retrieve j

    myDate = JSON.parse(j)

    d = new Date(myDate)

    ==========================================

    Thanks in advance to anyone who can provide the simplest way to do this
    without new imports,
    visits to github, or a lesson in date formatting (already been to the
    dentist this month).

    -G
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 21, '16 at 3:28p
activeMay 21, '16 at 9:55p
posts7
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase