Psl
Psl

Reputation: 3920

extract time from datetime using javascript

how can i extract time from datetime format.

my datetime format is given below.

var datetime =2000-01-01 01:00:00 UTC;

I only want to get the time 01.00 as 01

Upvotes: 47

Views: 169751

Answers (10)

Hishan_98
Hishan_98

Reputation: 210

I hope you can find a solution here.

let preDateTime = new Date("2022-03-31 22:26:00");
let newTime = preDateTime.toLocaleTimeString('en-US');
let hour = newTime.split(":")[0];
let amPm = newTime.split(" ")[1];
let seconds = newTime.split(":")[2].replace(amPm,'');;

let noAmPm = newTime.replace(amPm,'');
let noAmPmSeconds = noAmPm.replace(":"+seconds,'');
let noSeconds = newTime.replace(":"+seconds,' ');

if(parseInt(hour)<9){
    newTime = "0"+newTime;
    noAmPm = "0"+noAmPm
    noSeconds= "0"+noSeconds
    noAmPmSeconds = "0"+noAmPmSeconds;
}

console.log(newTime); //10:26:00 PM
console.log(noAmPm); //10:26:00 
console.log(noSeconds); //10:26 PM
console.log(noAmPmSeconds); //10:26

Upvotes: 0

Ayden Cheong
Ayden Cheong

Reputation: 31

I have done it! It looks like this:

console.log(new Date().toLocaleTimeString() + " " + new Date().getSeconds() + " seconds");

Upvotes: 3

Andrew
Andrew

Reputation: 1120

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString

Date.prototype.toLocaleTimeString() Returns a string with a locality sensitive representation of the time portion of this date based on system settings.

var time = datetime.toLocaleTimeString();

Update:

The new locales and options arguments let applications specify the language whose formatting conventions should be used and customize the behavior of the function. In older implementations, which ignore the locales and options arguments, the locale used and the form of the string returned are entirely implementation dependent.

// Depending on timezone, your results will vary
var event = new Date('August 19, 1975 23:15:30 GMT+00:00');

console.log(event.toLocaleTimeString('en-US'));
// expected output: 1:15:30 AM

console.log(event.toLocaleTimeString('it-IT'));
// expected output: 01:15:30

Upvotes: 77

George Moik
George Moik

Reputation: 465

with the moment.js library it works in this way:

var result = moment().format('hh');
alert(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>

Upvotes: 6

chungtinhlakho
chungtinhlakho

Reputation: 930

var date1 = new Date(1945,10,20, 17,30)
var date2 = new Date(1970,1,8, 12,00)

console.log(date1.getHours() - 8 + (date1.getMinutes()/60))

console.log(date2.getHours() - 8 + (date2.getMinutes()/60))

Upvotes: 3

subodh
subodh

Reputation: 6158

What about these methods

new Date().getHours()

new Date().getMinutes()

For example:

 var d = new Date();
 var n = d.getHours();

Edited

Return the hour, according to universal time:

new Date().getUTCHours()

Example:

var d = new Date();
var n = d.getUTCHours(); 

Upvotes: 22

Anujith
Anujith

Reputation: 9370

var datetime = ("2000-01-01 01:00:00 UTC");
var d1 = new Date(datetime);
var minute = d1.getUTCMinutes();
var hour = d1.getUTCHours();
if(minute > 0)  
     alert(hour+"."+minute);
else
     alert(hour);

Demo

Upvotes: 3

Code Lღver
Code Lღver

Reputation: 15593

Use the following code:

var datetime = "2000-01-01 01:00:00 UTC";

var dt = new Date(datetime);
var hr = dt.getUTCHours();
if(hr > 12) {
   hr -= 12;
}
alert(hr);

refer this link also.

Upvotes: 2

Bergi
Bergi

Reputation: 665545

Assuming you have a Date object like

var datetime = new Date("2000-01-01 01:00:00 UTC"); // might not parse correctly in every engine
// or
var datetime = new Date(Date.UTC(2000, 0, 1, 1, 0, 0));

then use the getUTCHours method:

datetime.getUTCHours(); // 1

Upvotes: 1

Mark Walters
Mark Walters

Reputation: 12400

As an alternative if you want to get the time from a string -

var datetime ="2000-01-01 01:00:00 UTC";
var myTime = datetime.substr(11, 2);
alert(myTime) //01

Upvotes: 9

Related Questions