-2

Please i need some help in javascript or jQuery :

how to get current date and time to minutes?

i have a datetime in that format : 'd/m/Y H:m' (example 25/12/2016 22:35) in input . how to get it and convert it to minutes ? and make difference between it and current datetime

Thank you in advance for help. I tried all but nothing works

AmenzO
  • 409
  • 7
  • 19

2 Answers2

0

To get the current date and time to minutes, you could divide the milliseconds:

var theMinutes = new Date().getTime() / (1000 * 60);

To get the input and convert it to minutes, you could use a Date object, and divide the milliseconds:

    var someDateTime = document.getElementById("someDateTime").value;
var someParts = someDateTime.split(" ");
var theDate = someParts[0];
var theTime = someParts[1];
var dateParts = theDate.split("/");
var timeParts = theTime.split(":");
var theDay = dateParts[0];
var theMonth = dateParts[1];
var theYear = dateParts[2];
var theHours = timeParts[0];
var theMinutes = timeParts[1];
var someDateTimeObject = new Date(theYear, theMonth, theDay, theHours, theMinutes, 0 );
var someDateTimeObjectMillis = someDateTimeObject.getTime();
var someDateTimeObjectMinutes = someDateTimeObjectMillis / (1000 * 60);
Useless Code
  • 12,123
  • 5
  • 35
  • 40
Kevin Field
  • 114
  • 1
  • 6
  • Just so you know, [`Date.now()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now) does the same thing as `new Date().getTime()`. It has pretty good browser support, IE9+ and fairly old versions of the other browsers. – Useless Code Nov 01 '15 at 06:40
  • Thank you that helped me much – AmenzO Nov 02 '15 at 00:00
0

Hello thank you for all yours answers

Firsly i get the current time using php ( in Milliseconds) then convert pickup date to milliseconds and get differences between them in hours : promo (in minutes) i get 767 hours normally i should get : 47 hours and some minutes

 var startdate=document.getElementById('pickupdate').value; //03-11-2015 15:49


var someParts = startdate.split(" ");
var theDate = someParts[0];
var theTime = someParts[1];
var dateParts = theDate.split("-");
var timeParts = theTime.split(":");
var theDay = dateParts[0];
var theMonth = dateParts[1];
var theYear = dateParts[2];
var theHours = timeParts[0];
var theMinutes = timeParts[1];
var someDateTimeObject = new Date(theYear, theMonth, theDay, theHours, theMinutes, 0 );
var someDateTimeObjectMillis = someDateTimeObject.getTime();
//var someDateTimeObjectMinutes = someDateTimeObjectMillis / (1000 * 60);


alert(someDateTimeObjectMillis);
var maintenant = <?php echo(round(microtime(true) * 1000)); ?>;
alert(maintenant);
var diff = someDateTimeObjectMillis - maintenant;
alert(diff);

var promo = diff/(60 * 1000);
alert(promo); // 46079  

thank you for your support

AmenzO
  • 409
  • 7
  • 19