Gatsby: Add option to get date in local or specified time zone

Created on 17 Feb 2019  路  4Comments  路  Source: gatsbyjs/gatsby

Summary

Add option to get date in local or specified time zone.

Basic example

Add option to date type.

  query blogListQuery($skip: Int!, $limit: Int!) {
    ...
    allMarkdownRemark(...) {
      edges {
        node {
          ...
          frontmatter {
            date(formatString: "MMMM DD, YYYY", local: true) <-- use local time zone
            date(formatString: "MMMM DD, YYYY", tz: "Asia/Tokyo") <-- use specified time zone
          }
        }
      }
    }
  }

and/or add option to gatsby-config.js.

Motivation

Display date in an easy-to-understand way for main readers of the website.

stale? needs more info

Most helpful comment

Thank you for your comment. I didn't know I can get raw string of the date if formatString is not specified.

https://github.com/gatsbyjs/gatsby/blob/445f5ff7508ab613967e18b32490e3feffd7730c/packages/gatsby/src/schema/types/type-date.js#L120

I could display date in local time zone using following query and page.

  query blogListQuery($skip: Int!, $limit: Int!) {
    ...
    allMarkdownRemark(...) {
      edges {
        node {
          ...
          frontmatter {
            date
          }
        }
      }
    }
  }
import moment from 'moment'

<small>{moment(node.frontmatter.date).local().format(`MMMM DD, YYYY`)}</small>

Therefore, I think that applying time zone in query is not absolutely necessary. However, if Gatsby supports formatString, it is more natural to be able to specify time zone of formatted time.

All 4 comments

Why do you want to do this inside the query and not inside the page or component itself?

Thank you for your comment. I didn't know I can get raw string of the date if formatString is not specified.

https://github.com/gatsbyjs/gatsby/blob/445f5ff7508ab613967e18b32490e3feffd7730c/packages/gatsby/src/schema/types/type-date.js#L120

I could display date in local time zone using following query and page.

  query blogListQuery($skip: Int!, $limit: Int!) {
    ...
    allMarkdownRemark(...) {
      edges {
        node {
          ...
          frontmatter {
            date
          }
        }
      }
    }
  }
import moment from 'moment'

<small>{moment(node.frontmatter.date).local().format(`MMMM DD, YYYY`)}</small>

Therefore, I think that applying time zone in query is not absolutely necessary. However, if Gatsby supports formatString, it is more natural to be able to specify time zone of formatted time.

Hiya!

This issue has gone quiet. Spooky quiet. 馃懟

We get a lot of issues, so we currently close issues after 30 days of inactivity. It鈥檚 been at least 20 days since the last update here.

If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!

Thanks for being a part of the Gatsby community! 馃挭馃挏

Hey again!

It鈥檚 been 30 days since anything happened on this issue, so our friendly neighborhood robot (that鈥檚 me!) is going to close it.

Please keep in mind that I鈥檓 only a robot, so if I鈥檝e closed this issue in error, I鈥檓 HUMAN_EMOTION_SORRY. Please feel free to reopen this issue or create a new one if you need anything else.

Thanks again for being part of the Gatsby community!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

3CordGuy picture 3CordGuy  路  3Comments

andykais picture andykais  路  3Comments

theduke picture theduke  路  3Comments

KyleAMathews picture KyleAMathews  路  3Comments

ghost picture ghost  路  3Comments