# Date comparison & timezone offset help

**URL:** <https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016>\
**Category:** support\
**Created:** [July 16, 2020, 5:38pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016 "2020-07-16T17:38:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Hobday](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexander_hobday/32/8422_2.png) [@Alexander\_Hobday](https://discourse.gohugo.io/u/Alexander_Hobday)\
**Post date:** [July 16, 2020, 5:38pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/1 "2020-07-16T17:38:18Z")

</div>

Can someone help me with this date comparison?

Is it possible to work out if this is before or after now?

Tue Oct 13 2020 10:00 AM PT

eg {{ if gt .date now }}

* * *

**Anyway interested there is an excellent explanation of timezones with Hugo at the bottom of this thread.**

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [July 16, 2020, 7:51pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/2 "2020-07-16T19:51:00Z")

</div>

How accurate does it have to be? Can we round down to the nearest day, or do you need hour:min granularity?

---

<div class="post-metadata">

**Author:** ![AdamSpann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/adamspann/32/11931_2.png) [@AdamSpann](https://discourse.gohugo.io/u/AdamSpann)\
**Post date:** [July 17, 2020, 10:59am UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/3 "2020-07-17T10:59:30Z")

</div>

convert to unix time.

{{ if gt .Date.Unix now.Unix }}

---

<div class="post-metadata">

**Author:** ![Alexander\_Hobday](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexander_hobday/32/8422_2.png) [@Alexander\_Hobday](https://discourse.gohugo.io/u/Alexander_Hobday)\
**Post date:** [July 17, 2020, 2:47pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/4 "2020-07-17T14:47:33Z")

</div>

I tried the unix timestamp conversion but I’m getting the following error:

at \<$date.Unix\>: can’t evaluate field Unix in type string

I think the issue is my date field is coming from a data file so I guess Hugo is considering it a string and not a date. How do I make sure it gets converted?

Here’s the data file:

data:  
- Tue Jul 21 2020 10:00 AM PT  
- Tue Aug 11 2020 10:00 AM PT  
- Tue Sep 15 2020 10:00 AM PT  
- Tue Oct 13 2020 10:00 AM PT

Here’s my code:

{{ $datafile := index $fields “data” }}  
{{ range $datafile }}  
{{ $date := . }}  
{{ if gt $date.Unix now.Unix }}  
{{ . }}  
{{ end }}  
{{ end }}

---

<div class="post-metadata">

**Author:** ![moorereason](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/moorereason/32/592_2.png) [@moorereason](https://discourse.gohugo.io/u/moorereason)\
**Post date:** [July 17, 2020, 2:51pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/5 "2020-07-17T14:51:19Z")

</div>

Try

```auto
{{ if gt (time .date) now }}

```

---

<div class="post-metadata">

**Author:** ![Alexander\_Hobday](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexander_hobday/32/8422_2.png) [@Alexander\_Hobday](https://discourse.gohugo.io/u/Alexander_Hobday)\
**Post date:** [July 17, 2020, 3:05pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/6 "2020-07-17T15:05:38Z")

</div>

Partially worked, no error but now the condition never evaluates to true.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [July 17, 2020, 3:13pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/7 "2020-07-17T15:13:23Z")

</div>

Back to my original question:

> [@jmooring](#):
>
> How accurate does it have to be? Can we round down to the nearest day, or do you need hour:min granularity?

---

<div class="post-metadata">

**Author:** ![Alexander\_Hobday](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexander_hobday/32/8422_2.png) [@Alexander\_Hobday](https://discourse.gohugo.io/u/Alexander_Hobday)\
**Post date:** [July 17, 2020, 3:13pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/8 "2020-07-17T15:13:56Z")

</div>

Nearest day works.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [July 17, 2020, 3:21pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/9 "2020-07-17T15:21:07Z")

</div>

You have to wrangle the date from your external source into a format understood by go. The partial referenced below uses an inline partial, so you’ll need Hugo 0.74.1.

content/foo,md:

```auto
{{< test "Tue Oct 13 2020 10:12 PM PT" >}}

```

layouts/shortcodes/test.html:

```auto
{{- $input := "" -}}
{{- with .Get 0 -}}
  {{- $input = . -}}
{{- else -}}
  {{- $msg1 := printf "The '%s' shortcode requires one positional parameter, a timestamp" .Name -}}
  {{- $msg2 := printf "formatted as follows: Tue Oct 13 2020 10:12 PM PT. See %s" .Position -}}
  {{- errorf "%s %s" $msg1 $msg2 -}}
{{- end -}}

{{- $timestamp := partial "convert-date.html" $input -}}

{{- $str_timestamp := $timestamp.Format "2006-01-02T15:04:05-07:00" -}}
{{- $str_now := now.Format "2006-01-02T15:04:05-07:00" -}}

<pre>
Input: {{ $input }} (time zone will be ignored)<br>
Converted: {{ $str_timestamp }}<br>
Now: {{ $str_now }}
</pre>

<br>

{{- if gt $timestamp now -}}
<pre>The input date is greater than now.</pre>
{{- else -}}
<pre>The input date is less than or equal to now.</pre>
{{- end -}}
</p>

```

layouts/partials/convert-date.html:

```auto
{{/*
@param string timestamp
  Example: Tue Oct 13 2020 10:12 PM PT

@returns time converted_timestamp
  Example: 2020-10-13T22:12:00

The time zone is ignored.
*/}}
{{- $time_parts := split . " " -}}

{{- $year := index $time_parts 3 -}}
{{- $month := partial "get-month-number" (index $time_parts 1) -}}
{{- $day := index $time_parts 2 -}}
{{- $hour := index (split (index $time_parts 4) ":") 0 -}}
{{- $min := index (split (index $time_parts 4) ":") 1 -}}
{{- $sec := "00" -}}
{{- $period := index $time_parts 5 -}}
{{- $tz := index $time_parts 6 -}}
{{- $offset := "00:00" }}

{{- if eq (lower (index $time_parts 5)) "pm" -}}
  {{ $hour = string (add (int $hour) 12) }}
{{- end -}}

{{- $converted_timestamp := printf "%s-%s-%sT%s:%s:%s-%s" $year $month $day $hour $min $sec $offset -}}

{{- return time $converted_timestamp }}

{{/*
@param string month_name
  Examples: mar, march, Mar, March

@returns string month_number
  Example: 03
*/}}
{{- define "partials/get-month-number" -}}
  {{- $months := dict -}}

  {{- $months = merge $months (dict "jan" "01") -}}
  {{- $months = merge $months (dict "feb" "02") -}}
  {{- $months = merge $months (dict "mar" "03") -}}
  {{- $months = merge $months (dict "apr" "04") -}}
  {{- $months = merge $months (dict "may" "05") -}}
  {{- $months = merge $months (dict "jun" "06") -}}
  {{- $months = merge $months (dict "jul" "07") -}}
  {{- $months = merge $months (dict "aug" "08") -}}
  {{- $months = merge $months (dict "sep" "09") -}}
  {{- $months = merge $months (dict "oct" "10") -}}
  {{- $months = merge $months (dict "nov" "11") -}}
  {{- $months = merge $months (dict "dec" "12") -}}

  {{- $month_name := substr . 0 3 | lower -}}
  {{- $month_number := index $months $month_name -}}

  {{- return $month_number -}}
{{- end -}}

```

---

<div class="post-metadata">

**Author:** ![Alexander\_Hobday](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexander_hobday/32/8422_2.png) [@Alexander\_Hobday](https://discourse.gohugo.io/u/Alexander_Hobday)\
**Post date:** [July 17, 2020, 3:23pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/10 "2020-07-17T15:23:57Z")

</div>

OK let me take a look through. I do have the option of changing the date in the date file to some other format if that would help Go view it as a date object?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [July 17, 2020, 3:26pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/11 "2020-07-17T15:26:46Z")

</div>

Yes, it would. For example:

```auto
2020-07-17T11:26:14-04:00

```

---

<div class="post-metadata">

**Author:** ![Alexander\_Hobday](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexander_hobday/32/8422_2.png) [@Alexander\_Hobday](https://discourse.gohugo.io/u/Alexander_Hobday)\
**Post date:** [July 17, 2020, 3:34pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/12 "2020-07-17T15:34:17Z")

</div>

That did the trick, my condition is now evaluating correctly

For reference:

Data file:

data:  
- 2020-07-21T10:00:00-08:00  
- 2020-08-11T10:00:00-08:00  
- 2020-09-15T10:00:00-08:00  
- 2020-10-13T10:00:00-08:00

Code:

{{ if gt (time .data ) now }}

Only issue I have now is formatting the date back to what I need:

This: {{ dateFormat “Tue Jul 21 2020 10:00 AM PT” . date}}

Outputs: Tue Jul 118 11110 80:00 AM PT

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [July 17, 2020, 3:45pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/13 "2020-07-17T15:45:46Z")

</div>

`dateFormat "Mon Jan 2 2006 3:04 PM MST"`

Note that “PT” is not a valid time zone. On the west coast of the United States, the time zone is either PST or PDT.

---

<div class="post-metadata">

**Author:** ![Alexander\_Hobday](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexander_hobday/32/8422_2.png) [@Alexander\_Hobday](https://discourse.gohugo.io/u/Alexander_Hobday)\
**Post date:** [July 17, 2020, 3:48pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/14 "2020-07-17T15:48:01Z")

</div>

> [@jmooring](#):
>
> Mon Jan 2 2006 3:04 PM MST

Thank you, that mostly did it, but the timezone isn’t getting translated:

Tue Aug 11 2020 10:00 AM -0800

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [July 17, 2020, 3:53pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/15 "2020-07-17T15:53:16Z")

</div>

What is the current offset of your system? I suspect it is not -08:00. Please confirm.

---

<div class="post-metadata">

**Author:** ![Alexander\_Hobday](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexander_hobday/32/8422_2.png) [@Alexander\_Hobday](https://discourse.gohugo.io/u/Alexander_Hobday)\
**Post date:** [July 17, 2020, 4:08pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/16 "2020-07-17T16:08:24Z")

</div>

I’m just testing locally, How would I find that out?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [July 17, 2020, 4:10pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/17 "2020-07-17T16:10:57Z")

</div>

On Linux:

```auto
date -Iseconds

```

---

<div class="post-metadata">

**Author:** ![Alexander\_Hobday](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexander_hobday/32/8422_2.png) [@Alexander\_Hobday](https://discourse.gohugo.io/u/Alexander_Hobday)\
**Post date:** [July 17, 2020, 4:45pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/18 "2020-07-17T16:45:07Z")

</div>

> [@jmooring](#):
>
> What is the current offset of your system

Ahh, sorry you meant my machine. I’m GMT -6 currently.

---

<div class="post-metadata">

**Author:** ![Alexander\_Hobday](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/alexander_hobday/32/8422_2.png) [@Alexander\_Hobday](https://discourse.gohugo.io/u/Alexander_Hobday)\
**Post date:** [July 17, 2020, 4:49pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/19 "2020-07-17T16:49:09Z")

</div>

I actually don’t know how Hugo processes timezones. If you have any pointers, appreciated.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [July 17, 2020, 5:17pm UTC](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016/20 "2020-07-17T17:17:33Z")

</div>

### Short Answer

If you want to ignore the offset in your dates, and always print PT, do this:

```auto
dateFormat "Mon Jan 2 2006 3:04 PM PT"

```

### Medium Answer

Don’t display the time zone abbreviation. Display the offset instead. Why? Because time zone abbreviations are essentially useless.

```auto
dateFormat "Mon Jan 2 2006 15:04 -07:00"

```

### Long Answer

There is a N:N relationship between time zone abbreviations and offsets. For example:

Different offsets with the same time zone abbreviation:

| Time Zone Abbreviation | Description | Offset |
| --- | --- | --- |
| CST | Central Standard Time (North America) | -06:00 |
| CST | China Standard Time | +08:00 |
| CST | Cuba Standard Time | -05:00 |

Different time zone abbreviations with the same offset:

| Offset | Description | Time Zone Abbreviation |
| --- | --- | --- |
| -06:00 | Central Standard Time (North America) | CST |
| -06:00 | Mountain Daylight Time (North America) | MDT |
| -06:00 | Easter Island Standard Time | EAST |
| -06:00 | Galápagos Time | GALT |

So, when the timestamp is `2020-07-17T12:49:12-04:00`, which time zone abbreviation should be displayed? It depends on where you are. Similarly, if you say you eat dinner at 5 PM CST, I have no idea when you actually eat dinner.

With Hugo, if you use the `Mon Jan 2 2006 3:04 PM MST` format string:

- When the offset of the date to be formatted is the same as your local offset, the local time zone abbreviation for the offset is displayed, based on the TZ environment variable on your machine. If you change the TZ environment variable on your machine to `America/Los_Angeles` and rebuild the site, you will get different results.
- When the offset of the date to be formatted is different than your local offset, we have no idea what time zone abbreviation to display, so we display the offset instead.

So what to do?

> Don’t display the time zone abbreviation. Display the offset instead.

References:

- [https://spin.atomicobject.com/2016/07/06/time-zones-offsets/](https://spin.atomicobject.com/2016/07/06/time-zones-offsets/)
- [https://en.wikipedia.org/wiki/List\_of\_time\_zone\_abbreviations](https://en.wikipedia.org/wiki/List_of_time_zone_abbreviations)
- [https://community.intersystems.com/post/setting-tz-environment-variable-linux](https://community.intersystems.com/post/setting-tz-environment-variable-linux)

[Next page](https://discourse.gohugo.io/t/date-comparison-timezone-offset-help/27016.md?page=2)
