-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?

7
  • look for a comma in the string and trim everything before it including comma Commented Jul 17, 2018 at 9:12
  • @ShubhamAgarwalBhewanewala can u show code Commented Jul 17, 2018 at 9:14
  • @Gnanaseelan are you looking for formatting dates ?! Commented Jul 17, 2018 at 9:14
  • @HyyanAboFakher no ...i have like this 2018-07-17T09:03:08.819Z" in side the array . i need to split to 2018-07-17 like this Commented Jul 17, 2018 at 9:17
  • @Gnanaseelan, So you can convert a string to the Date object using formatter. Commented Jul 17, 2018 at 9:18

6 Answers 6

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);

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

3 Comments

@Gnanaseelan, this cannot convert string from 2018-07-17T09:03:08.819Z to 2018-07-17. Are you sure about your question?
@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, 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.
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

Comments

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);

Comments

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];

Comments

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.

Comments

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.

2 Comments

this out put only like 2018-07-17?
I just adjusted the answer to fit your needs exactly, with little code :)

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.