Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript/jQuery - DateTime to Date and Time separate strings

Is there any simple way to convert the following:

2011-08-31T20:01:32.000Z

In to UK date format: 31-08-2011

and time to: 20:01

like image 504
CLiown Avatar asked Sep 27 '11 07:09

CLiown


People also ask

How to get time from string in JavaScript?

JavaScript Date toLocaleTimeString() The toLocaleTimeString() method returns the time portion of a date object as a string, using locale conventions.

How to get time from date and time in JavaScript?

JavaScript Date getTime() getTime() returns the number of milliseconds since January 1, 1970 00:00:00.

How to get time stamp from date?

Getting the Current Time Stamp If you instead want to get the current time stamp, you can create a new Date object and use the getTime() method. const currentDate = new Date(); const timestamp = currentDate. getTime(); In JavaScript, a time stamp is the number of milliseconds that have passed since January 1, 1970.

How do you separate date and time in HTML?

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


2 Answers

You can use momentjs (http://momentjs.com/):

var date = moment(dateObject).format("YYYY-MM-DD");
var time = moment(dateObject).format("HH:mm:ss"); 
like image 77
Bitclaw Avatar answered Oct 25 '22 00:10

Bitclaw


You can use jquery-dateFormat plugin. The following should do the trick:

$.format.date('2011-08-31T20:01:32.000Z', "dd-MM-yyyy"));
$.format.date('2011-08-31T20:01:32.000Z', "hh:mm"));
like image 32
alh84001 Avatar answered Oct 24 '22 22:10

alh84001