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:
var date = "1st December,2016";
var arr1 = date.split(' ');var arr2 = arr1[1].split(',');
console.log('date: ', arr1[0]);console.log('month: ', arr2[0]);console.log('year: ', arr2[1]);
Convert datetime to date and time format separately in JS
let date=new Date(document.querySelector('#myDate').value)
Date part ===> date.toLocaleDateString() .
Time part ====> date.toLocaleTimeString() for the time part.
from below answer:
Split the date and time in two elements
Related Topics
How to Count the Entire Number of Rows in a Datatable
Download Image as File in Typescript
How to Clear All Input Fields in Bootstrap Modal When Clicking Data-Dismiss Button
Node.Js MySQL Query in a for Loop
How to Format Numbers in Vuejs
Reactjs - Multiple If Conditions Inside Map Function
Access a Variable Outside of .Then Function
How to Make Autoplay of the Swiper Slider Start Only After the Slider Enters Viewport
How to Write an Onclick Function for a Dynamically Generated Button
How to Mute Video With JavaScript or Jquery
Show Multiple Divs on Button Click
Looping Through a Json Object Containing More Objects and Arrays
How to Wait for Element to Disappear in Cypress
Jquery Click Events Firing Multiple Times
Reactjs: Expected Onclick Listener to Be a Function, Instead Got Type String
How to Make a Owl Carousel With Arrows Instead of Next Previous