How to Split Date and Time from the Datetime Variable in JavaScript

how to split date to formatted date and time

Try this:

<script type="text/javascript">
var string = "20160509125119040";
var date = string.substr(0,4) + "-" + string.substr(4,2) + "-" + string.substr(6,2) + " " + string.substr(8,2) + ":" + string.substr(10,2) + ":" + string.substr(12,2);
console.log(date);
</script>

How do I split the date and time into two elements?

Use date.toLocaleDateString() for the date part, and date.toLocaleTimeString() for the time part.

Separate date and time

In java , better way is

String dateString = "2013-09-01 16:20";
String[] split = dateString .split(s);
System.out.println(split[0]+" at "+split[1]);

0th element is your date string and 1th element is time string.

If you are confused with java and javascript then

var dateString = "2013-09-01 16:20";
var split = dateString .split(s);
consol.log(split[0]+" at "+split[1]);

Split time from date value in jquery

Use .split() to get what you are looking for

.split() turns the values into an array starting at 0.

so if we did date = date.split('T') then data[0] is = "2017-05-21" and data[1] is = "00:00:00"

var date = "2017-05-21T00:00:00";date = date.split('T')[0];
console.log(date)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Splitting date in Javascript

If you could make sure that your string always has that format, the simplest way is to use 2 split command: