Home » Php » javascript – Extract hour and minute from date function

javascript – Extract hour and minute from date function

Posted by: admin February 25, 2020 Leave a comment


I want to pass the time variable extracted from the database as a Date function in jQuery and want to extract hours and minutes from it and want to store it in a javascript variable.

var time="<?php echo $row['time']; ?>";
var time=new Date(time);
var hrs=time.getHours();
var min=time.getMinutes();

Please find if there is any error in this code.

How to&Answers:

Without more knowledge I can’t really give you an answer.
But its likely that you’re expecting a Date Object, when in reality your DB is returning something else.

(Maybe a string?, a number?)

Make sure what type of data is exactly storen in your “time” variable. Would be my suggestion because I dont see errors in the code. Must be the logic

Really hope this helped but more insight into what your DB is doing would help getting a clear answer 🙂

Good Luck !


You could find answers for this all over Stackoverflow or read the docs on the date object.

However, here is an answer you might like

var myMinutes = 1; // This represent 1 minute subtraction
var myHours   = 60; // This represent 60 minutes subtraction which is 1 hour
var dateMSubObject= new Date(oldDateObject.getTime() - myMinutes*60000); //Substracting your minutes variable
var dateHSubObject= new Date(oldDateObject.getTime() - myHours*60000); //Substracting your hours variable

To make it more managable you could do hours like this aswell for etc 24 hours

var myHours   = 60*24; // This represent 24 hours subtraction

You could also make the above code a function which wil take paramters like units, type and from that return your desired result

The 60000 part is milliseconds and represents 1 minute.

And welcome to StackOverflow, if you take some time exploring the website you will quickly be able to find the most common questions usually followed by great answers 🙂


This did it for me:

let jsdate = new Date(unixtimestamp+1000);

example in use:

let a = new Date();
console.log("a ="+a)

let stamp = a.getTime()
console.log("stamp ="+stamp) // timestamp

let newTimeObj = new Date(stamp*1000)
console.log("newTimeObj :::::::"+newTimeObj)




You need to convert the UNIX Timestamp(in seconds) which is coming from your PHP code to milliseconds, and then pass it as a parameter to a Date object.

Consider the below example in JavaScript:

//UNIX Timestamp from your PHP code
let timeInUNIXTimeStamp = "1581653281"; 

// Create a new JavaScript Date object based on the timestamp
// Multiplied by 1000 to convert it into milliseconds, from seconds, as Date object works in milliseconds
var date = new Date(timeInUNIXTimeStamp * 1000);

// Get Hours part from the timestamp
var hours = date.getHours();

// Get Minutes part from the timestamp
var minutes = "0" + date.getMinutes();

// Will display time in H:M format
var timeInHMSFormat = hours + ':' + minutes.substr(-2);


You can same achieve in PHP also, there you need to convert UNIX Timestamp to H:M format, using the date() function, where the first parameter will the format you wanted and the second will be your UNIX Timestamp.

Example: date("h:i", 1581653281);
Where h is hours, in 12-hours format
            i is minutes

Read move about PHP’s data() function Date function in php

Consider PHP the code below, inside your JavaScript:

   var time="<?php echo date('h:i', $row['time']); ?>";

   //Now Split the above string into array
   var timeArray = time.split(":");

   var hours = timeArray[0];
   var minutes = timeArray[1];

For more detail see this answer Convert UNIX Timestamp