How to add 1 hour and 5 minutes in Javascript - javascript

I have a requirement to show the pop up of every 1 hour 5 seconds, have set the time for 1 hour, how to set a time for 1 hour 5 seconds using Javascript.
for 3 minutes set the time for 3 * 60 * 1000;
for 1 hour set the time for 1 * 60 * 60 * 1000;
how to be for 1 hour 5 minutes?

Use date object and it's methods. getTime() will get the current date in milliseconds and add appropriate milliseconds for 1 hour 5 minute (which is 65 minute), which will be 65 * 60 * 1000. And after use setTime() to create the date according to the milliseconds.
var date = new Date();
console.log(date);
date.setTime(date.getTime() + 65 * 60 * 1000);
console.log(date);

1 Hr s= (1 * 60 * 60* 1000)
5 Sec = (5*1000)
Then
1 Hrs 5 Sec = (1 * 60 * 60* 1000)+(5*1000)

Related

How to check that the user has selected 3 years before date in the textbox in JavaScript

I am new to coding I have confused about the concept of Dates in JavaScript. I need to validate that the user has selected 3 years before's date in the textbox*, but my codes through some errors.* Check The Following Code:
<script>
function run() {
var dt =document.getElementById("dt").value; //Value of selected date
var ndt = new Date(); //Current Date
var diff = ndt.getTime()-dt.getTime();
var milliseconds = 1000 * 60 * 60 * 24;
var result = diff/milliseconds;
if (result > 1095) {
alert("Correct");
} else {
alert("Wrong")
};
}
</script>
Your calculations should be something along the lines:
if (diff > 1000 * 60 * 60 * 24 * 365 * 3)
This is because: 1000ms (is 1 second) * 60 (equals 1 minute) * 60 (equals 1 hour) * 24 (equals one day) * 365 (equals 1 year, roughly) * 3 (equals 3 years).
Surely that's pretty prone to errors! Leap-years are not taken to account, and many more.
I'd suggest you also check this one if you decide to go on a more-stable path: How can I calculate the number of years between two dates?

How to countdown and divide the time into segments?

I wish to do a countdown to a specific date and hour (January 10, 2018, 19:30). Which in large part I am able to do. Below code shows the remaining days, hours, minutes and seconds.
The tricky bit is to get certain periods of time. The countdown should respond to the following:
1. on the deadline day and hour show the message 'Now going live'. Which is 10 January 2018 19:30.
2. That same day but BEFORE 19:30 it should say 'Going live tonight'
3. The complete day before the deadline day (from 00:00 to 23:59) it should say 'last day'
4. The complete days before that it should say 'many days to go'
Step 1 and 2 I managed, but I'm having trouble getting the complete day before the deadline day and the complete days before that. That's because I'm not able to define the complete day before the deadline day (and the days before that). Because it counts '1 day' as 1 day before 10 January 19:30 (so it also takes those hours/minutes of 19:30 into account).
Step 1 and 2 I managed in the if-loop, but I can't figure out how to do step 3 and 4. Step 3 should say something like 'count one day, but before 10 January 2018 00:00. So it should subtract that 19:30 to get to 9 januari 2018 00:00-23:59. And the same for step 4. Can someone fix my code?
// Get todays date and time
var now = new Date().getTime();
// Set the date we're counting down to
var countDownDate = new Date("Januari 10, 2018 19:30").getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result
this.timeleft.text = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
// countdown day 19:30
if ((days == 0) && (hours == 0) && (minutes == 0)) {
this.countdown.text = "NOW GOING LIVE!";
// countday day 00:00 - 19.30
} else if ((days == 0) && (hours <= 19) && (minutes <= 30)) {
this.countdown.text = "GOING LIVE TONIGHT!";
// 9 January 00:00 - 23:59
} else if ((days <= 1) && (hours >= 19) && (minutes >= 30)) {
this.countdown.text = "LAST DAY";
// days before 10 January
} else if (days >= 1) {
this.countdown.text = "MANY DAYS TO GO";
}
Since the "deadline" is hard-coded, you can hard-code everything and end up with something very simple:
var now = new Date().getTime();
var lastDayThreshold = new Date("January 9, 2018 00:00").getTime();
var liveTonightThreshold = new Date("January 10, 2018 00:00").getTime();
var countDownDate = new Date("January 10, 2018 19:30").getTime();
if (now < lastDayThreshold) this.countdown.text = "MANY DAYS TO GO";
else if(now < liveTonightThreshold) this.countdown.text = "LAST DAY";
else if(now < countDownDate) this.countdown.text = "LIVE TONIGHT";
else this.countdown.text = "NOW GOING LIVE";
Alex's answer was indeed what I was after. Those 'treshhold times' did the trick. Was thinking about improving it though as now I have to hard-code three dates/times. Preferably I would like to only specify the countDownDate date/time. And then let both Threshold dates calculate themselves. I tried to do that in a way, but ran into a problem. I know how to specify one day (1000 * 60 * 60 * 24), so I could subtract this 'oneday' value to get to the day before. But I wasn't able to calculate the milliseconds for the specified time 19:30. In order to read the miilliseconds since the beginning of January 10 until January 10 19:30. If I were able to do that it would look something like this (though I know this is incorrect, but you'll get the idea):
var oneday = 1000 * 60 * 60 * 24;
var countDownDate = new Date("January 10, 2018 19:30").getTime();
var lastDayThreshold = new Date(countDownDate - oneday "00:00").getTime();
var liveTonightThreshold = new Date(countDownDate "00:00").getTime();
You'll see my problem: for lastDayTreshold I could subtract one day of the countdowndate but then it would consider that 19:30 the previous day, not 00:00. And for liveTonightThreshold I also couldn't specify that I mean 00:00 of the countdowndate.
Would there be a way of doing that? Then I would just have to specify the countdown day and time and the rest would figure them out themselves.

loop value in format time(hour and minute)

How to display the value, using for loop in JavaScript?
ex: if i loop number 0 < 2 then i get result like this:
0
1
my question how i make the loop like this:
0:0
0:1
0:2
0:3
0:4
to
0:59
1:0
1:1
1:2
1:3
1:4
to
1:59
because the last loop is 1,how i make +1 so the last loop result like this:
0:0
0:1
0:2
0:3
0:4
to
0:59
1:0
1:1
1:2
1:3
1:4
to
1:59
2:0
thank you
There are (at least) a couple of ways of doing this.
Perhaps the easiest would be to have two nested loops, one for the minute and one for the second, along the lines of the following pseudo-code:
for minutes = 0 to 59 inclusive:
for seconds = 0 to 59 inclusive:
output minutes ":" seconds
That won't necessarily scale well as you move up to hours and days and it means you have to work on a minute-boundary. In other words, you can't easily do 752 seconds (12m32s) unless you do some monstrosity like:
for minutes = 0 to 11 inclusive:
for seconds = 0 to 59 inclusive:
output minutes ":" seconds
for seconds = 0 to 32 inclusive:
output "12:" seconds
Perhaps a better way, which alleviates those problems,would be to work exclusively with seconds and translate that to more complete units on the fly:
for fullsecs = 0 to 752 inclusive:
secsleft = fullsecs
seconds = fullsecs % 60
secsleft = secsleft - seconds
minutes = (secsleft / 60) % 60
secsleft = secsleft - minutes * 60
hours = (secsleft / 3600) % 24
secsleft = secsleft - hours * 3600
days = secsleft / 86400
output days ":" hours ":" minutes ":" seconds
What that snippet within the loop does is to strip out the "odd" seconds (those that aren't a multiple of a minute) and then subtract those seconds from the value, so that the result is a multiple of a minute.
Then it does that, for other units as well, all the way up to days, using the fact that there are 60 seconds in a minute, 60 * 60 = 3600 in an hour, and 24 * 60 * 60 = 86400 in a day.

How to return a string difference between two dates (current date and input date) in JavaScript?

I have a function, which is written in JavaScript for getting the date time in day/month/year hours/minutes/seconds AM/PM format. What I want is a function, which will allow me to return the string of the following type (1 second, or 5 seconds, or 2 minutes, or 4 hours, or 1 day, or 5 months, or 5 years). Here is a sample code snippet:
function (var inputDate)
{
var difference = dateNow - inputDate;
//1 second, or 5 seconds, or 2 minutes, or 4 hours, or 1 day, or 5 months, or 5 years - I want to return the highest difference between both dates (current date and the date, which I input)
return string difference;
}
Thanks in advance!
Once again I'm late to the party but as #user1950292 suggests you can subtract two dates and format the difference. Something like:
function dateDiffString(a,b) {
var diff = (a.getTime() - b.getTime());
var diffs = {
years : Math.floor((diff) / (1000 * 60 * 60 * 24 * 365)),
months : Math.floor((diff) / (1000 * 60 * 60 * 24 * 30)),
weeks : Math.floor((diff) / (1000 * 60 * 60 * 24 * 7)),
days : Math.floor((diff) / (1000 * 60 * 60 * 24)),
hours : Math.floor((diff) / (1000 * 60 * 60)),
mins : Math.floor((diff) / (1000 * 60)),
secs : Math.floor((diff) / (1000))
}
//iterate through diffs to find first number > 0
for (var prop in diffs) {
if(diffs.hasOwnProperty(prop) && diffs[prop] > 0){
return diffs[prop] + " " + prop;
}
}
}
var today = new Date();
var anotherDay = new Date("2013 12 13,11:52:39");
alert(dateDiffString(today,anotherDay));
http://jsfiddle.net/xuqNt/
You can create two Date objects and substract them. Then get the days, hours, minutes, seconds of the result and choose which to output.
I have no code atm, but the Date usage is quite intuitive.
https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Date

Purpose of date.getTime in the expires flag of the cookie?

I have a line of code that sets a cookie with an expiry date that looks like this.
var date = new Date();
date.setTime(date.getTime() + 1000*60*60*24*365);
var expires = "; expires=" + date.toGMTString();
What I'm trying to do is understand what each number represents. I know thats its just adding milliseconds to the time object but what does each one represent is the question.
1000 milliseconds in a second
60 seconds in a minute
60 minutes in an hour
24 hour in a day
365 days in a year
So, you'll get the quantity of milliseconds in a year.
var date = new Date(); // date object (now on this computer)
date.setTime( // change the time
date.getTime() // now in milliseconds since 1970
+ 1000 // milliseconds in a second
* 60 // seconds in a minut
* 60 // minutes in an hour
* 24 // hours in a day
* 365 // approximate days in a year. Total ~ number of milliseconds in a year
);
var expires = "; expires=" +
date.toGMTString(); // format the time to what the cookie likes
Please read
Date
toGMTString
for more information
1000 millicesonds times 60 seconds times 60 minutes times 24 hours times 365 days. It's a year.
Think about it like this:
1000*60*60*24*365
1000 // Converts milliseconds into seconds
60 // Converts seconds into minutes
60 // Convert minutes into hours
24 // Converts hours into days
365 // Convert days into year
1000 milliseconds per second,
60 seconds per minute,
60 minutes per hour,
24 hours in a day,
365 days in a year,
So it is adding the number of milliseconds in a year, advancing the time by a year

Categories

Resources