# Compare Time to Determine if Now is Today

**URL:** <https://discourse.gohugo.io/t/compare-time-to-determine-if-now-is-today/9217>\
**Category:** support\
**Created:** [November 14, 2017, 10:52am UTC](https://discourse.gohugo.io/t/compare-time-to-determine-if-now-is-today/9217 "2017-11-14T10:52:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon94969202](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@anon94969202](https://discourse.gohugo.io/u/anon94969202)\
**Post date:** [November 14, 2017, 10:52am UTC](https://discourse.gohugo.io/t/compare-time-to-determine-if-now-is-today/9217/1 "2017-11-14T10:52:45Z")

</div>

Given the following front matter:

```auto
sale_ends = 2017-11-14

```

Is there a way to determine if [`now`](https://gohugo.io/functions/now/) is the same day as `sale_ends`? I don’t mind exploring more on my own, but I’m not sure if there’s a simple way or if I should start reviewing the [Go docs](https://godoc.org/time) linked from `now`. Thank you!

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [November 14, 2017, 11:53am UTC](https://discourse.gohugo.io/t/compare-time-to-determine-if-now-is-today/9217/2 "2017-11-14T11:53:40Z")

</div>

I haven’t tried , but you should be able to use `time` function to convert that `sale_ends` string to `time.Time` format, and then `.Unix` to convert that time and `now` time to seconds, and then take a difference of those. You can know if the time is already past if the difference is negative/positive. Based on the difference amount, you can also tell if the `sale_ends` day is the same as ~~today~~ `now` (which is actually the date the site was last published, not necessarily _today_).

Related example: [https://gohugo.io/functions/unix/#example-time-passed-since-last-modification](https://gohugo.io/functions/unix/#example-time-passed-since-last-modification)

Also, I am assuming that you already looked at setting [`expirydate`](https://gohugo.io/variables/page/#page-variables) in the front-matter? If you want to just not publish a post beyond certain date, just use that variable in the front-matter so that you don’t need to do that date math.

---

<div class="post-metadata">

**Author:** ![budparr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/budparr/32/749_2.png) [@budparr](https://discourse.gohugo.io/u/budparr)\
**Post date:** [November 15, 2017, 3:43am UTC](https://discourse.gohugo.io/t/compare-time-to-determine-if-now-is-today/9217/3 "2017-11-15T03:43:20Z")

</div>

With .Date you can easily convert to Unix time for comparison to now (".Date.Unix" “ge” now.Unix), but it looks like you need to convert your field instead of a date parameter, so you could probably do something like `{{.Params.sale_end | dateFormat "20060102"}}` and use that to compare to now. You’ll have to convert `now` in whatever date format you choose to match (e.g. `{{now.Format "20060102"}}`). I think that will work.

---

<div class="post-metadata">

**Author:** ![tcgriffith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/tcgriffith/32/7932_2.png) [@tcgriffith](https://discourse.gohugo.io/u/tcgriffith)\
**Post date:** [November 15, 2017, 6:23am UTC](https://discourse.gohugo.io/t/compare-time-to-determine-if-now-is-today/9217/4 "2017-11-15T06:23:53Z")

</div>

I have a bit of code to highlight posts updated within 3 days, hope this will be helpful:

use YearDay is really convenient.

```auto
...
 {{ if lt (sub now.YearDay .Lastmod.YearDay) 3 }}
      <span class="last-modified">updated within {{ sub now.YearDay .Lastmod.YearDay }} days</span>
 {{ end }}
...

```

---

<div class="post-metadata">

**Author:** ![Jonathan\_Griffin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jonathan_griffin/32/8355_2.png) [@Jonathan\_Griffin](https://discourse.gohugo.io/u/Jonathan_Griffin)\
**Post date:** [November 8, 2018, 11:54am UTC](https://discourse.gohugo.io/t/compare-time-to-determine-if-now-is-today/9217/5 "2018-11-08T11:54:09Z")

</div>

@tcgriffith

Your solution does not work properly. It all goes haywire when you deal with different years. I was getting negative days for old posts.

This works better (my example displays a bootstrap badge, but the principal is the same):

```
{{ if lt (sub (now.Unix) (.Lastmod.Unix)) 1209600 }}
    <span class="badge badge-secondary">New</span>
{{ end }}
```

---

<div class="post-metadata">

**Author:** ![budparr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/budparr/32/749_2.png) [@budparr](https://discourse.gohugo.io/u/budparr)\
**Post date:** [November 8, 2018, 1:57pm UTC](https://discourse.gohugo.io/t/compare-time-to-determine-if-now-is-today/9217/6 "2018-11-08T13:57:54Z")

</div>

And note, I believe you have to have `enableGitInfo` set to true for `.Lastmod` to work.

---

<div class="post-metadata">

**Author:** ![Jonathan\_Griffin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jonathan_griffin/32/8355_2.png) [@Jonathan\_Griffin](https://discourse.gohugo.io/u/Jonathan_Griffin)\
**Post date:** [November 8, 2018, 3:35pm UTC](https://discourse.gohugo.io/t/compare-time-to-determine-if-now-is-today/9217/7 "2018-11-08T15:35:53Z")

</div>

> [@budparr](#):
>
> And note, I believe you have to have `enableGitInfo` set to true for `.Lastmod` to work.

That is not entirely correct. .Lastmod takes the date as follows:

- If `lastmod` is not set, and **`.GitInfo` feature is disabled** , the front matter `date` field will be used.
- If `lastmod` is not set, and **`.GitInfo` feature is enabled,** `.GitInfo.AuthorDate` will be used instead.
