-1

How to split data to get like 2018-07-17 from the following code:

getCompanyHolydayLeave() {
  this.props.actions.getCompanyLeaveDetails().then(() => {
    if (!this.props.common.getCompanyLeaveDetailsPending) {
     const poyaArray= this.props.common.companyLeave 
     var result = poyaArray.map((poyaArray) => poyaArray.date )
     var  xData= result.toString().split("T")

     this.setState({ companyLeave:xData });  
     }
  });
}

My output xdata like this.

0: "2018-04-29"

1: "18:30:00.000Z,2018-05-06"
​
2: "18:30:00.000Z,2018-05-28"
​
3: "18:30:00.000Z,2018-06-26"

I need like this:

0: "2018-04-29"

1: "2018-05-06"

How can I do so?

Mamun
  • 66,969
  • 9
  • 47
  • 59
Gnanaseelan
  • 394
  • 1
  • 5
  • 22

6 Answers6

6

You can format xData with map():

var xData = ["2018-04-29",
 "18:30:00.000Z,2018-05-06",
"18:30:00.000Z,2018-05-28",
"18:30:00.000Z,2018-06-26"];

xData = xData.map(d => d.split(',')[d.split(',').length-1]);
console.log(xData);
Mamun
  • 66,969
  • 9
  • 47
  • 59
  • @Gnanaseelan, this cannot convert string from `2018-07-17T09:03:08.819Z` to `2018-07-17`. Are you sure about your question? – Hardik Shah Jul 17 '18 at 09:33
  • @Mamun i got now like this (2018-07-17 ) format array ..now i need how to add one day with this array object dates below my out put of xData array ​0: "2018-04-29" 1: "2018-05-06" ​ 2: "2018-05-28" ​ 3: "2018-06-26" ​ 4: "2018-07-15" ​ 5: "2018-07-16" ​ 6: "2018-07-26" ​ 7: "2018-09-23" – Gnanaseelan Jul 17 '18 at 15:26
  • @Gnanaseelan, manipulating dates like this in JavaScript is quite difficult. Though you can try `map()` to modify the string date by splitting and modifying at the appropriate index....which is again not a smaller task to achieve..........thanks. – Mamun Jul 17 '18 at 16:24
3

you can split very easily on Coma (,). After that you need to apply format on date. Below is the code for that :

var x="18:30:00.000Z,2018-05-06";
var t=x.split(',') // it will give you array
new Date(t[1])

below you can apply formatter for date

Deepak Kumar
  • 648
  • 6
  • 14
3

You can use Date for that!

Change in input date format does not affect output format.


function convertDate(dateStr){
     var _tmp = new Date(dateStr);
     return _tmp.toISOString().split("T")[0];
}

Example:

var dateArray = ["2018-04-29","18:30:00.000Z,2018-05-06","18:30:00.000Z,2018-05-28","18:30:00.000Z,2018-06-26"]

function convertDate(dateStr){
   var _tmp = new Date(dateStr);
   return _tmp.toISOString().split("T")[0];
}

var newDateArray = dateArray.map(x=>convertDate(x));
console.log(newDateArray);
Rajan
  • 1,463
  • 12
  • 26
2

Say you have the value in xData. Just iterate over each value in xData and do:

xData[i] = xData.split[i](",")[1];

And you'll have your changed data in the xData variable.

Or if you want it to be a little clearer, you can go:

var temp = xData.split[i](",");
xData[i] = temp[1];
Akash Srivastav
  • 756
  • 5
  • 15
2

Using the Date class, you can create a dateformat like this:

let date = new Date("18:30:00.000Z,2018-05-06");
let dateString = date.toISOString().split("T")[0];

The constructor of the Date class understands all of your 0-4 examples of input using the Date-class constructor. The output is constructed using the date-part of the ISO standard datetime format.

Magnus Ingwersen
  • 374
  • 3
  • 11
2

This is how you can convert it your string 2018-07-17T09:03:08.819Z to Date.

var fromDate = new Date("2018-07-17T09:03:08.819Z");

var formattedDate = fromDate.getFullYear() + "-" + (fromDate.getMonth()+1) + "-" + fromDate.getDate();
console.log(formattedDate);

Please make sure while using this about the timezone.

Hardik Shah
  • 4,042
  • 2
  • 20
  • 41