1

I have an object containing time. For example x.time = 10:20:00. I want to take the current time minus my objects time.

This is my code so far, but i get the error message ="Invalid Date":

for(var i = 0; i<x.length; i++){
    nowDate = new Date();
    minutesLeft = new Date(nowDate.getFullYear(), nowDate.getMonth(),    nowDate.getDate() + x[i].time);
    text +- "It is "+ minutesLeft[i] + " milliseconds left";
 }
jaikl
  • 971
  • 2
  • 9
  • 23
  • _it is complaining at my code_ How ? – AxelH Jan 19 '17 at 12:10
  • 1
    _Questions seeking debugging help (**"why isn't this code working?"**) **must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it** in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]._ – AxelH Jan 19 '17 at 12:10
  • @AxelH Oh sorry. It is says: Invalid Date – jaikl Jan 19 '17 at 12:13
  • And how can we reproduce this ? What is `x` values (an array? ) ? `time` is a String ? and your last line should be in the loop if you want to use `i` – AxelH Jan 19 '17 at 12:15
  • @AxelH The x values are fetched from a API which a get in JSON. The value i´m using is time and the format is always "00:00:00" – jaikl Jan 19 '17 at 12:18
  • 1
    `text +- = "..."` <-- what are you expecting that `+-` to do? – Jamiec Jan 19 '17 at 12:18
  • You are trying to set the date of your new date to `1910:20:00` because `time` is a text value – AxelH Jan 19 '17 at 12:20
  • @AxelH. I see. But how to i use this string to create new date()? – jaikl Jan 19 '17 at 12:23

3 Answers3

0

In order to convert your time property into a date object you can do:

var timepieces = x.time.split(':');
var date = new Date();
date.setHours(timepieces[0]);
date.setMinutes(timepieces[1]);
date.setSeconds(timepieces[2]);

Then you can directly compare the two dates by using the getTime() method of the Date object.

nowDate.getTime() === date.getTime()
nowDate.getTime() > date.getTime()
nowDate.getTime() < date.getTime()

You can also get the difference of two dates in milliseconds:

var milliseconds = nowDate.getTime() - date.getTime();
Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
0

There are a bunch of problems with your code. I'll go through and hopefully catch everything.

  1. Use var to declare your variables inside your loop. (further reading)

  2. When you create the variable minutesLeft you are doing a bit of weird concatenation. You told us that x.time is a string such as "10:20:00" but you are (string) concatenating that with Date.prototype.getDate which returns a number in the range 1-31 (representing the day of the month). You are essentially doing this:

    minutesLeft = new Date(2017,0,1910:20:00);
    

    Which I hope you see will not create a new date. You perhaps wanted something along the lines of

    minutesLeft = new Date(2017,0,19, 10, 20, 0);
    

    Which should give you what you want (todays date set to the appropriate time defined by x.time.

  3. text +- does not make any sense. I suspect a typo, you meant text += which will append the value on the right to the variable text. Or, perhaps text = which will assign the value, replacing what was there

  4. "It is "+ minutesLeft[i] + " milliseconds left" using minutesLeft[i] will take a single character from a string (or an item from an array, if the value is an array). Yours is just a date object, and is not an array, so I suspect you just meant to leave off the [i] part altogether.

  5. If you're trying to get the difference between the current date/time and your selected date/time you need to do some arithmetic with nowDate and minutesLeft. I'm assuming this is a difference you're after.


var x = [{time:"10:20:20"}];
var text = "";
for(var i = 0; i<x.length; i++){
    var nowDate = new Date();
    var timeSplit = x[i].time.split(":");
    var minutesLeft = new Date(nowDate.getFullYear(), nowDate.getMonth(),    nowDate.getDate());
    minutesLeft.setHours(timeSplit[0]);
    minutesLeft.setMinutes(timeSplit[1]);
    minutesLeft.setSeconds(timeSplit[2]);
    text += "It is "+ (nowDate-minutesLeft) + " milliseconds left";
 }

console.log(text);
Community
  • 1
  • 1
Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

For example x.time = 10:20:00. I want to take the current time minus my objects time.

Your code seems quite confused, it seems you're trying to do the following:

var time = '10:20:00';
var timeParts = time.split(':');
var now = new Date();

// Current date and time
console.log(now.toString());

// Subtract the hours part of the time from the hours part of the current time
now.setHours(now.getHours() - timeParts[0]);

// Subtract the minutes part of the time from the minutes part of the current time
now.setMinutes(now.getMinutes() - timeParts[1]);

// Subtract the seconds part of the time from the seconds part of the current time
now.setSeconds(now.getSeconds() - timeParts[2]);

// Adjusted date and time
console.log(now.toString());

// You can set the time parts all in one go:
var now2 = new Date();
now2.setHours(now2.getHours() - timeParts[0],
              now2.getMinutes() - timeParts[1],
              now2.getSeconds() - timeParts[2]);
console.log(now2.toString());

Lastly, copying a date is as simple as:

var date1 = new Date();
var date2 = new Date(date1);
RobG
  • 142,382
  • 31
  • 172
  • 209