<p>Hello everyone,</p>
<p>I have been looking for a better library for date and time values than time.Time. I am storing information in Mongo which has worked great. However, I am running into problems with parsing HTML and input type date's and vice versa. I could format the date on the html side, but I would rather have a stronger time value that can handle more date formats. Any suggestions would be great. Thanks in advanced.</p>
<hr/>**评论:**<br/><br/>icholy: <pre><p>Read this <a href="https://golang.org/pkg/time/#pkg-constants" rel="nofollow">https://golang.org/pkg/time/#pkg-constants</a></p></pre>shovelpost: <pre><blockquote>
<p>However, I am running into problems with parsing HTML and input type date's and vice versa.</p>
</blockquote>
<p>What exactly are you trying to parse? The <code>time</code> package is actually very flexible and powerful. It just doesn't parse and format the same way that the other languages do.</p></pre>ptdave: <pre><p>The input dumps out mm/dd/yyyy. This far, I've had to modify each back and forth in JavaScript. Just looking to save a step.</p></pre>shovelpost: <pre><p>You can parse that with <code>time.Parse("01/02/2006")</code>. I do not understand why you need a 3rd party package for.</p></pre>ptdave: <pre><p>I'm marshalling a struct that contains the time.Time type. I then turn it around after a few checks and update/insert into a mongo database. </p></pre>shovelpost: <pre><blockquote>
<p>I'm marshalling a struct that contains the time.Time type. I then turn it around after a few checks and update/insert into a mongo database.</p>
</blockquote>
<p>That does not answer my question of why you need a 3rd party package for that. But in any case from what I understand using a custom type like <a href="/u/janderssen" rel="nofollow">/u/janderssen</a> suggested fixes your problem.</p>
<p>Another solution might be that instead of using <code>time.Time</code> in your struct to use <code>int64</code> and keep a Unix time. You are probably losing the nanoseconds anyways when you convert to/from JSON.</p></pre>ptdave: <pre><p>Looking for third party to avoid reinventing the wheel. But otherwise, yes I would agree with why would I need a third party library.</p></pre>recurrency: <pre><p>what formats are you looking for?</p></pre>ptdave: <pre><p>I'd like it to format to mm/dd/yyyy automatically from Json and back iso for mgo interface</p></pre>metakeule: <pre><p>have a look at
<a href="https://github.com/metakeule/fmtdate" rel="nofollow">https://github.com/metakeule/fmtdate</a> </p>
<p>its tiny and should do what you want</p></pre>janderssen: <pre><p>In the browser I use Moment, and in go I created a new type based on time.Time as follows (ensuring I use RFC3339)</p>
<pre><code>type CustomTime time.Time
func (t CustomTime) MarshalJSON() ([]byte, error) {
stamp := fmt.Sprintf("\"%s\"", time.Time(t).Format(time.RFC3339))
return []byte(stamp), nil
}
func (t *CustomTime) UnmarshalJSON(data []byte) error {
// for legacy, you could probably ignore this.
s := strings.Trim(string(data), "\"")
if s == "null" {
*t = CustomTime(time.Time{})
return nil
}
// if we fail to parse the RFC3339, fall back to the Nano version
temp, err := time.Parse(time.RFC3339, s)
if err != nil {
temp, err = time.Parse(time.RFC3339Nano, s)
if err != nil {
*t = CustomTime(time.Time{})
return nil
}
}
*t = CustomTime(temp)
return nil
}
</code></pre>
<p>Not sure if this is the best solution in the world, but works great for me.</p>
<p>Cheers</p></pre>ptdave: <pre><p>Thanks. Hate doing repetition unnecessarily. I'll give this a try</p></pre>ptdave: <pre><p>I'm noticing a lot of people giving me negative post values. I assure you, I wouldn't be asking if I didn't need a solution. The raw values out of HTML input are not being parsed by the time.Time type natively when my struct is pulled. </p>
<p>So I am not sure why people are being so negative on this.</p></pre>shovelpost: <pre><blockquote>
<p>I'm noticing a lot of people giving me negative post values.</p>
</blockquote>
<p>My best guess is because your post/question lacks details but most importantly you are blaming the <code>time</code> package instead of trying to solve your problem or at least explain it us so we can help.</p>
<blockquote>
<p>The raw values out of HTML input are not being parsed by the time.Time type natively <strong>when my struct is pulled</strong>.</p>
</blockquote>
<p>What does that mean? Did you try the code of <a href="https://www.reddit.com/r/golang/comments/6o2ya3/better_timetime/dkelhsj/" rel="nofollow">this</a> comment? Write a simple example on Go playground of what you are trying to do and what doesn't work so we can talk with some code.</p></pre>ptdave: <pre><p>I was only looking for a package. I didn't think code was necessary when I'm looking for a few suggestions.</p>
<p>I am using him web framework and when I bind the body to a struct, unless I manually preformat it in my angular4 typescript, it will error from the date formatting in HTML. </p></pre>itsmontoya: <pre><p>Why not just work with UNIX timestamps?</p></pre>ptdave: <pre><p>I have data already that I have to keep in a mongo. I could do that, but if I understand correctly, I would have to go through each existing value and rebuild it.</p></pre>nemith: <pre><p>I've had great luck with <a href="https://github.com/araddon/dateparse" rel="nofollow">https://github.com/araddon/dateparse</a> to try to parse just about any format.</p></pre>markuspeloquin: <pre><p>I came here hoping to see people hating on the insane format specifiers.</p>
<p>I did read through format.go, which is pretty efficient-looking, and am a little curious if pre-constructed formatter objects are any better. Maybe not as much of a benefit as pre-constructing regular expressions.</p>
<p><em>Edit</em> the real money is in merging parser patterns so I don't have to write a parser for each possible number of fractional digits.</p></pre>gott_modus: <pre><blockquote>
<p>but I would rather have a stronger time value that can handle more date formats.</p>
</blockquote>
<p>why not just write in c then, man. </p></pre>ptdave: <pre><p>cause it's c. </p></pre>