0

I need help in parsing out the following data.


Data to parse:

2014-09-08 00:00:00:000

2014-09-15 00:00:00:000

2005-12-12 00:00:00:000

I have already created a piece of code to parse the month.

  var dohMonth = jQuery(this.attr("ows_DOH").split('-')[1];

The above code will create the following output:

09

09

12

Now I need help creating a piece of code for parsing the actual day of the month.

If I use:

var dohDay = jQuery(this).attr("ows_DOH").split('-')[2];

It will create the following output:

08 00:00:00:000

15 00:00:00:000

12 00:00:00:000

The extra zeros (The time format) are what I do not need.

I would like it to just be:

08

15

12

Any suggestions on how I should go about this ?

1
  • 2
    Are you parsing because you want to work with dates? Commented Feb 18, 2016 at 19:01

3 Answers 3

2

Turning the text into a javascript date object and then extracting out the data should do it for you.

var tempDate = new Date(jQuery(this).attr("ows_DOH"));
var dohDay = tempDate.getDate();
var dohMonth = tempDate.getMonth() + 1; // 0 indexed
var dohYear = tempDate.getFullYear();
Sign up to request clarification or add additional context in comments.

Comments

0

Do your first split on the blank space ' ', this will give you the date and time as separate pieces. Then just split the date on '-' to get the year, month, and day.

Comments

0

You can directly get the date by splitting the remaining text again with respect to the spaces and then extracting the 0th element.

var dohDay = (jQuery(this).attr("ows_DOH").split('-')[2]).split(' ')[0];

But rather than extracting the date components from a string, you should consider converting that string to a Javascript Date/Time Object. You could then use the predefined functions to extract individual date components directly rather than splitting this string multiple times.

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.