This is a creation in Article, where the information may have evolved or changed.
JavaScript + Golang Processing time issues
Today, a colleague asked JS and Golang processing time problem always have errors, so think of last year to do a project also appeared this problem, send a blog record:
Directly on the code:
javascript fragment: var sa = $ ( ' #sa ' ). Datetimebox ( ' getValue ' ); var ea = $ ( ' #ea ' ). Datetimebox ( ' getValue ' ); //at this time the SA and EA outputs are YYYY-MM-DD HH:mm:ss this standard format string ------------------------------------ -----var SA1 = new date (SA). toisostring (); var ea1 = new Date (EA). Toisostring (); //at this time SA1 and ea1 need to be ISO-8601 format: yyyy-mm-ddthh:mm:ss.sssz
golang片段: sa1 := form["sa"][0] ea1 := form["ea"][0] fmt.Println("sa1: ", sa1) fmt.Println("ea1: ", ea1) sa, _ := time.Parse(time.RFC3339Nano, sa1) ea, _ := time.Parse(time.RFC3339Nano, ea1) fmt.Println("sa: ", sa) fmt.Println("ea: ", ea)
There is no inconsistency between the front-end time and the time saved in the database after the above processing.
Browser Console output:
Golang Program:
Golang output:
Database storage:
*
Some browsers may have incompatible issues with the ISO time, just add the following code before converting the ISO time:
if(!Date. prototype.toisostring) {Date. prototype.toisostring = function() { function pad(n) { returnN <Ten?' 0 '+ N + +}return This. getUTCFullYear () +'-'+ Pad ( This. getUTCMonth () +1) +'-'+ Pad ( This. getUTCDate ()) +' T '+ Pad ( This. getUTCHours ()) +':'+ Pad ( This. getUTCMinutes ()) +':'+ Pad ( This. getUTCSeconds ()) +'.'+ Pad ( This. getUTCMilliseconds ()) +' Z '; } }
*
I hope you can help!