1

I have a string that includes date and time together. I need all of them seperated in some cases. My string is like this : 28/08/2015 11:37:47

There are some solutions in there but they do not fix my problem

var date = "12/15/2009";
var parts = date.split("/");
alert(parts[0]); // 12
alert(parts[1]); // 15
alert(parts[2]); // 2009

This is similar thing but as I said above, I need to split all of them.

Thank you for your help

3 Answers 3

2

If you always have that datetime format I'd suggest a simple regex

"28/08/2015 11:37:47".split(/\/|\s|:/)

This splits it on a / a space and the colon

and will return

 ["28", "08", "2015", "11", "37", "47"]

Edit as per question asked in comment

function parse() {
  /** INPUT FORMAT NEEDS TO BE DAY/MONTH/YEAR in numbers for this to work **/
  var datetime= document.getElementById("datetime").value;
  var time = document.getElementById("time").value;
  var output = document.getElementById("output")
  var datetimearr = datetime.split(/\/|\s|:/);
  var timearr = time.split(/:/);
  var date = new Date(datetimearr[2],datetimearr[1]-1,datetimearr[0],datetimearr[3],datetimearr[4],datetimearr[5]);
  date.setHours(date.getHours()+(parseInt(timearr[0])));//parseInt needed otherwise it will default to string concatenation
  date.setMinutes(date.getMinutes()+(parseInt(timearr[1])));
  date.setSeconds(date.getSeconds()+(parseInt(timearr[2])));
  output.value = date.toString();
}
Date time: <input type="text" value="28/08/2015 11:37:47" id="datetime"><BR/>
Time to add: <input type="text" value="11:37:47" id="time"><input type="button" value="calculate!" onclick="parse()"><BR/>
<textarea  id="output" placeholder="Output comes here" style="width:400px;height:100px;"></textarea>

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

7 Comments

Thank you, this is a great solution for me
The website blocks me to open a new title in 90 minutes. Do you know the solution of this problem -> Adding (hh:mm:ss) to (YYYY/mm/dd hh:mm:ss) in Javascript
sorry, you want to do what? in a date object or in a string object?
I added a snippet to my answer. take a look at it. I really need to know what you want to do... Do you wish to modify an array, create a string or create a date object?
I am gonna get this objects in html form, and read them in js with getElementById function. To imagine; var datetime= document.getElementById("datetime1").value; var time= document.getElementById("time1").value; var sumofdatetime= datetime+time; I hope, i could clarify it. Namely as math example: Input1: 01/01/2015 12:00:00 Input2: 28:10:00 output: 02/01/2015 16:10:00
|
2

You can use regular expressions.

var str = '28/08/2015 11:37:47';

console.log(str.split(/[\/:\s]/));

1 Comment

Was just about to answer but you got there, so I won't bother. This is what I would use! +1
1

You can simply convert the string to date by:

var datestr = "12/15/2009 11:33:44";
var date = new Date(datestr);

And now access the values by those functions:

date.getDate(); //  -> 15
date.getMonth(); //  -> 12
date.getFullYear(); //  -> 2009
date.getHours(); //  -> 11
date.getMinutes(); //  -> 33
date.getSeconds(); //  -> 44

working js fiddle example here

EDIT: Had to fix example because document.write didn't work correctly on Fiddle.

1 Comment

That is not a standard date format so this is not guaranteed to work in all environments.

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.