Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. React date formatting

React date formatting

Scheduled Pinned Locked Moved JavaScript
javascripthelpquestion
4 Posts 4 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Simon_Whale
    wrote on last edited by
    #1

    What is the best way in react to convert a date from

    2022-09-04T00:00:00

    into

    04/09/2022

    Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

    P Richard DeemingR J 3 Replies Last reply
    0
    • S Simon_Whale

      What is the best way in react to convert a date from

      2022-09-04T00:00:00

      into

      04/09/2022

      Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      I don't know about react, but the general answer is to use the date parser to convert to an internal time representation (eg Unix time, seconds since 1/1/1970...) then the date formatter to get what you want.

      Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

      1 Reply Last reply
      0
      • S Simon_Whale

        What is the best way in react to convert a date from

        2022-09-04T00:00:00

        into

        04/09/2022

        Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Probably by using the Intl.DateTimeFormat[^] object, which is supported in all modern browsers.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        • S Simon_Whale

          What is the best way in react to convert a date from

          2022-09-04T00:00:00

          into

          04/09/2022

          Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

          J Offline
          J Offline
          Jeremy Falcon
          wrote on last edited by
          #4

          JavaScript's native date object does that.

          const date = new Date(Date.parse('2022-09-04T00:00:00'));
          console.log(date.toLocaleString());

          I would suggest putting a Z at the end of the ISO input date though, just to make it explicit that it's in UTC.

          const date = new Date(Date.parse('2022-09-04T00:00:00Z'));
          console.log(date.toLocaleString());

          Otherwise, JavaScript will assume the input is in the local time zone.

          Jeremy Falcon

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups