2

I want to display dates (range of dates). The format of dates coming from DB seperated by hyphen is

Sep 21, 2017 - Sep 29, 2017

I want to convert it to the format

09/21/2017 - 09/25/2017

How can I convert this?

3
  • Have a look at moment.js Commented Sep 20, 2019 at 4:49
  • The two dates are coming in a single field from Db seperated by a hyphen. Commented Sep 20, 2019 at 4:52
  • Please post your code so far and detail exactly what isn't working Commented Sep 20, 2019 at 4:54

6 Answers 6

3

You can use this function that converts to your format: Your CodeSand

const convert = dateRange =>
    dateRange.split(" - ")
        .map(date => new Intl.DateTimeFormat('en-US').format(new Date(date)))
        .join(" - ")

console.log(convert("Sep 21, 2017 - Sep 29, 2017"));

The code below works and uses only pure JavaScript ES6.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use Intl.DateTimeFormat object.

const dbDate = new Date("Sep 21, 2017")

console.log(new Intl.DateTimeFormat('en-US').format(dbDate))

From the MDN Docs -

The Intl.DateTimeFormat object is a constructor for objects that enable language-sensitive date and time formatting.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat

Edit - If you are getting both dates in an single string you can split() it out and then apply the above method and concatenate (based on your requirement).

2 Comments

never knew about Intl, +1
JS is so vaaaaaast :)
2

enter image description hereReactJS have it's own date library which is moment date library. It can Parse, validate, manipulate, and display dates and times in JavaScript.

So use npm to install react-moment along with its peer dependency, moment by running this command :

npm install moment

Then Import and use in your Typescript file

import * as moment from 'moment';
const date = moment("Sep 21, 2017").format('MM/DD/YYYY');
console.log(date); // 09/21/2017

Comments

0

Moment.js mentioned in your comments has helped me a lot with the situation, specifically for what you want to do is found here

And moment.js package for reactjs can be installed from here

this will definitely help with your formating issue

Comments

0
const date = "Sep 21, 2017 - Sep 29, 2017"


const dateConvertor = date => {


    const splited = date.split(" ")

    const dateItems = []
    splited.forEach((item,index) => {
            if(item.indexOf(",") === -1) {
                dateItems.push(item)
            } else {
                dateItems.push(item.slice(0,item.indexOf(",")))
            }
     })

     let convertedDate = "";

     dateItems.forEach((item,index) => {
        if(index === 1 || index === 5) {
            convertedDate += "/"+item+"/"
        } else if (index === 3) {
            convertedDate += " "+ item + " "
        } else {
            convertedDate += item
        }
    })

    return convertedDate
}


dateConvertor(date)

Comments

0

First separate the string so that you can use it:

const [from, to] = '09/21/2017 - 09/25/2017'.split(' - ');

This will give you 2 strings, not formatted as you want so you will need to do some further transformation (assuming momentjs is available):

const [from, to] = '09/21/2017 - 09/25/2017'.split(' - ').map(item => 
moment(item).format('DD-MM-YYYY'));

If a slight format difference is OK, you might not need moment and use it like this

const [from, to] = '09/21/2017 - 09/25/2017'.split(' - ').map(item => 
new Intl.DateTimeFormat('en-US').format(new Date(item), {month: "2-digit", day: "2-digit"}));

NOTE: I am using 2-digit param here, which is supposed to provide the date / month as mentioned (09 instead of 9) but there seems to be an issue with that at least in some browsers.

Some explanation if not obvious: The split part, creates an array. The map part, will transform the 2 strings to formatted date objects. The first part (left of the =) is destructuring the array and giving you the variables to use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.