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?
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?
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.
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).
Intl, +1
ReactJS 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
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)
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.