Is there an easier way of getting a datetime from a string such as this: "29 March, 2014" in JAVASCRIPT
I've done it within PHP, but I want to fiddle with some front end to add days to the date etc..
Here is the required PHP code to achieve this:
function removeDateComma($string){
$without_comma = preg_replace('/,/', '', $string);
return $without_comma;
}
function getYearMonthDayFromDate($string){
$array = array();
$string = removeDateComma($string);
$string = strtotime($string);
$year = date('Y', $string);
$month = date('m', strtotime("-1 month", $string));
$day = date('d', $string);
$array['year'] = $year;
$array['month'] = $month;
$array['day'] = $day;
return $array;
}
Calling this to get the required datetime:
$from = getYearMonthDayFromDate($_REQUEST['from_date']);
Can anyone come up with a solution to this problem? Getting the same result but in javascript?
Edit:
It's already implemented in PHP. I need to do it at the front end i.e. no form submission, I cant send anything anywhere, i've got to get this date from an input field, i.e. var field = document.getElementById("input_from").value;
so that will be in the format 11 Month, 2014. So i've got this string sitting here which is useless, I need to get each individual value, i.e. the year: 2014, month: 03 or 3 and day: 11
Thanks!