-4

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.

Not A Bot
  • 2,474
  • 2
  • 16
  • 33

4 Answers4

0

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 :)

ii iml0sto1
  • 1,654
  • 19
  • 37
0

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 !

  • It's a timestamp variable. It is stored as 9:30:00. I want to store this data as a javascript variable and pass is to date function in javascript. Later, I need to extract only hours and minutes from it and print as 9:30. – Mohammed Arif Feb 13 '20 at 13:56
  • In JavaScript, a timestamp is the number of milliseconds that have passed since January 1, 1970... Ive been working on a selfmade-calendar the past few months, so im kinda worked into the Date Object of Javascript. Ill' post it in a separate answer so other people can find it too ^^ – coding_cate Feb 13 '20 at 14:59
0

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)

OUTPUT::::

Output

  • if i was any help to anyone, i'd appretiate an upvote. Im still new here and im working really hard on getting some reputation points <3 !! – coding_cate Feb 13 '20 at 15:10
  • Just a hint. You do not have to concat results in the console as string. It is more simple to separate it. Like `console.log("a =", a)`. This way you can actually investigate outputed objects instead of those being stringyfied. Also try to make a snippet out of the code so people can run it an see the result inline themselves. Lastly comment your steps.. like "why do I add 1000"? – Lain Feb 14 '20 at 22:21
  • Wow thank you I didnt know I could log results like that ! I thought what I posted was already a cnipped but I'm gonna look that up thank you so much for the feedback ! Also, yeah I see, people should understand what im doing since this is a helping plattform. Thanks so much for the feedback its VERY apreciated – coding_cate Feb 17 '20 at 07:54
0

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);

console.log(timeInHMSFormat);

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

Not A Bot
  • 2,474
  • 2
  • 16
  • 33