I would like to provide a list of about 40 positive numbers and then have my home page display the first number. Then at midnight, the number will change to the next number in the list. When at the end of the list, the rotation starts over. So for instance, a user goes to my page several times today and sees the first number in the list. Then say 1:00am, they go back to the page and see the next number on the list and will do so until midnight tomorrow night...etc etc etc Is this possible?
I've tried several different javascripts that does change the number according to my list BUT when a user goes to the page, it starts the list over again.
I am so new to this, I don't know which part of my code you might need since what I have been trying does what its supposed to do...just not how I want it to do.
Do I make sense?
Unfortunately, I won't be able to use php for this webpage.
You can use new Date().getTime() to get the current time in milliseconds. If you convert that to days, and round down, you can find the number of days since the epoch. Take the modulo of that and use it to obtain the index of your array of numbers. Since you're always using the current time (according to the user), you don't need to store any cookies or server-side counter.
var nums = [1,1,2,3,5,8,13]; // and so on
d = new Date(), // today
days = d.getTime() / (1000*60*60*24),
idx = Math.floor(days) % nums.length;
alert(nums[idx]); // should change once a day
Define a start date (preferably in the past), calculate today's date, subtract the two, and then use modulo to squeeze it into the range of numbers you want to show:
var minDate = 15681, // days since 1-1-1970
nowDate = Math.ceil(new Date().getTime() / 1000 / 86400),
numbers = [1, 5, 1234, 6543, 1236456];
// get the number for today
console.log(numbers[(nowDate - minDate) % numbers.length]);
If the start date doesn't matter, you can simplify the expression to:
numbers[Math.ceil(new Date().getTime() / 86400000) % numbers.length];
Btw, this won't change at midnight for everyone btw, because .getTime() gives GMT time.
Related
I am trying to calculate the time duration of a tasks, that I get from an ajax response.
Following are my table values:
Jobid techid, techtype, notes, starttime, stoptime
1 1 Brakes Break disc needed to be changed 2020-07-16 13:00:00 2020-07-16 13:40:00
1 2 Oil Change Replaced oil 2020-07-17 08:00:00 2020-07-17 09:00:00
1 3 Cleaning Cleaned the vehicle 2020-07-17 10:00:00 2020-07-17 10:30:00
On my ajax response, in the above case, I am getting 3 objects each having the start time, and stop time. I want to calculate total time spent in hours and minutes.
Is there an easy way to calculate the total duration?
With a string like 2020-07-16 13:00:00 you can construct a JS Date and get the milliseconds since the UNIX epoch with getTime() like so
new Date('2020-07-16 13:00:00').getTime()
Or, if you prefer, as pointed out by #Yousaf in the comments you can actually just use the - operator with Dates directly and get the millisecond difference
// resolves to 3600000, or 1 hour in milliseconds
new Date('2020-07-16 13:00:00') - new Date('2020-07-16 12:00:00')
Using that, you can get the difference in milliseconds between any two dates, and convert that to hours / minutes / whatever with straightforward arithmetic.
You can simply use Date to construct a date and then minus the start time from the end time.
Here I use getTime to get the millisecond difference, divide by 1000 to get seconds and divide by 60 to get minutes.
You could also use getMonth and such if you have bigger differences.
const starttime = '2020-07-16 13:00:00'
const stoptime = '2020-07-16 13:40:00'
const duration = new Date(stoptime) - new Date(starttime)
console.log(duration / 1000 / 60)
[UPDATE]
I think you can check this answer, but basically you should convert each date to js Date, get the milliseconds and just calculate endtime - startime.
const timelapse = new Date(endtime).getTime() - new Date(startime).getTime();
From there, you transform that in the unit you need (e.g: seconds = milliseconds/1000);
Sorry, my bad for writing fast.
I'm using Bootstrap-Timepicker to collect a time field. The user will select the the start time with the timepicker widget as well as a number of iterations and a time interval. I want to add the selected interval to the time for the given number of iterations.
For example: if the User select 11:40:00 PM and wishes to ad 10 minutes and 15 seconds 4 times, then I need to iterate through the following time values:
11:40:00 PM
11:50:15 PM
12:00:30 AM
12:10:45 AM
Bootstrap-timpicker doesn't provide an easy way to manipulate the time value like this that I know of, and when I get its value it returns a String formatted h:i:s A as in the example above.
I figure there must be an easier way to this than the way I plan to solve this problem currently which would be to parse the value and run if conditions to check to see if the hour or AM/PM roll over. Should I convert this string to a datetime, and if so, how? I'm open to other suggestions as well.
Put in the time, with a date, into the JavaScript date object like this:
var myDate = new Date('11:40:00 PM 2015-09-23');
Then, add minutes
myDate.setMinutes(myDate.getMinutes() + 10);
Then add seconds
myDate.setSeconds(myDate.getSeconds() + 15);
And this can be repeated 4 times
You can get the time back out in a format you'd like, just look at the docs
You can also add the time at once, but this is less pretty I think
myDate.setTime(myDate.getTime() + 615);
I have an interesting result from the javascript in an Acrobat PDF Form
I have a series of date form fields. The first field is for user entry and the remaining fields are calculated by javascript, each field incremented by one day.
The code is:
var strStart = this.getField("userField").value;
if(strStart.length > 0) {
var dateStart = util.scand("dd/mm/yy",strStart);
var dateStartMilli = dateStart.getTime();
var oneDay = 24 * 60 * 60 * 1000 * 1; // number of milliseconds in one day
var dateMilli = dateStartMilli + oneDay;
var date = new Date(dateMilli);
event.value = util.printd("dd/mm/yy",date);
} else { event.value = "" }
The issue is if I input 05/04/15 in to the user field the result is 05/04/15 (same, wrong) while any other date of the year correctly increments by one day (ie 25/10/15 gives 26/10/15, 14/2/15 gives 15/2/15 etc)
The same error occurs on the 3rd of April 2016, 2nd of April 2017, etc (ie each year)
I have a fortnight (14) of these incrementing fields, each incrementing the date from the previous calculated field with the same javascript as above ("userField" is changed to date2, date3, date4 etc). What is very strange is that the next field that increments off the second of the two 05/04/15 correctly returns 06/04/15 and there isn't an issue after that.
Does anyone know why this might be?!
That doesn't happen on my browser's JavaScript engine and/or in my locale, so it must be an Acrobat thing or that date may be special in your locale (e.g., DST).
In any case, that's not the correct way to add one day to a JavaScript date, not least because some days have more than that many milliseconds and some have less (transitioning to and from DST).
The correct way is to use getDate and setDate:
var strStart = this.getField("userField").value;
if(strStart.length > 0) {
var dateStart = util.scand("dd/mm/yy",strStart);
dateStart.setDate(dateStart.getDate() + 1); // Add one day
event.value = util.printd("dd/mm/yy",dateStart);
} else { event.value = "" }
setDate is smart enough to handle it if you go past the end of the month (per specification).
If it's DST-related, the above will fix it. If it's some weird Acrobat thing, perhaps it will work around it. Either way, it's how this should be done.
Let me guess, that's the day daylight savings starts in your locale? 24 hours after midnight is not always the next day, because some days have 25 hours.
Approaches that come to my head:
manipulate the day. (This is easy if Acrobat allows dates like the 32nd of January, because oyu can just increment the day. Otherwise, maybe don't bother because leap years aren't much better than DST.)
don't start from midnight. If you never use the hour and minute within the day, don't pin your day at the strike of midnight, but at, say, 3am. After a change in DST status, later days in your fortnight might register as 2am or 4am, but as long as you're only using the day…
I have tried to save a specific time into my mongodb database
with the javascript date object like:
var currenttime = new Date();
currenttime.setHours(14);
currenttime.setMinutes(37);
db.test.insert({time: currenttime});
however I have noticed that not only are the hours and minutes saved,
but also the date. I am searching for a way to only save the hours and
minutes, but in a way that I can still do less than greater than operations
on it. Is there a way to do this?
MongoDb Date is only 64bits, on the other hand if you will store your time as just 2 32 bit integers (hours and minutes) you will be already using these 64 bits. If you will save them as a 4 letters string, it will be even more.
So you can not gain space advantage. But you will lose advantage of querying your data. It will be harder to find all elements that are bigger than particular time with 2 numbers format and even harder with strings.
So I would save them as dates. If you really need only time - and need to query by this time, you can do the following trick: make all dates the same. For example:
var a = new Date(); // gives current date and time (note it is UTC datetime)
a.setYear(2000);
a.setMonth(0);
a.setDate(1);
db.test.insert({time: currenttime});
This way all the elements will have the same date and different time. In such a way you sort them properly. Also if you need to find all the elements where time is smaller than a particular time, you can quickly create a date object with year/month/day (2000/0/1) and query your data properly.
You can consider to save number of minutes as an integer field.
For example, 8:30 will be converted as 8 hour * 60 minutes + 30 minutes = 510 minutes. Save 510 as an number in MongoDB.
Basically you can try these two options to save time:
Save time as type: String
const userInput = '05:20';
const hours = userInput.slice(0, 2);
const minutes = userInput.slice(3);
Save date time in type: Date
But in second option you have to create Date object and set hours and minutes:
const date = new Date(dateString);
date.setHours(hours, minutes);
I have a field in my database which states the timezone of the user. It is an integer field so anything > 0 is a + hour.
I need to display two separate times on the users screen
e.g.
Your time is: "just get the current computer time"
The other users time is: "get the current computer time then either add or subtract hours based on the database record"
I can do the database side of things I just need help with the Javascript element. For demonstration purposes I am happy for you to hard code the offset
The time must update automatically every 60 seconds to reflect the new minute and hour
Thanks
You can just set the hours on the date object in JavaScript. For example:
var date = new Date();
date.setHours((new Date()).getHours() - 5);
It will also try and be clever and make adjustments for you, as MDN says:
If a parameter you specify is outside of the expected range, setHours
attempts to update the date information in the Date object
accordingly. For example, if you use 100 for secondsValue, the minutes
will be incremented by 1 (min + 1), and 40 will be used for seconds.
You can use setInterval to perform the update. Here's a more complete example:
setInterval(function() {
var local = new Date();
var remote = new Date();
remote.setHours(local.getHours() - 5);
$('#time-local').html(local.toGMTString());
$('#time-ny').html(remote.toGMTString());
},1000);