I have this webpage that takes in an input in seconds, and produces what the time will be after the time has elapsed. So I went about it using the following code
let duration = prompt("Enter your trip duration in seconds: "); // get the time from the vaue of the input element
let newtime = new Date(); // get the current time
newtime.setSeconds(newtime.getSeconds() + duration)
I decided to test the code and it gave me weird results and for some reason when I divided the duration by ten it started to work. Even though it worked I wasn't satisfied. So I added some more code to try and find where the bug is.
let oldtime = new Date();
oldtime.setSeconds(oldtime.getSeconds() + 60);
I added some code so that it would show on the webpage to compare the two times and these are my results when I enter 60 as the duration...
Just a little summary. The code works when I hard code the values in but it behaves weirdly if I try get the input from a prompt
You are concatenating a string to a number instead of adding. You have to parse the result from prompt to a number (with the unary plus operator):
let duration = prompt("Enter your trip duration in seconds: "); // get the time from the vaue of the input element
let newtime = new Date(); // get the current time
newtime.setSeconds(newtime.getSeconds() + +duration)
console.log(newtime)
I believe the issue is because you need to parse the duration from a string to number. So, try this
newtime.setSeconds(newtime.getSeconds() + parseInt(duration, 10))
You can use a library like Luxon to deal with dates and times without having to worry about unexpected behaviour like the one you described.
import { DateTime } from "luxon";
const now = DateTime.now();
const timeAfterTrip.plus({ seconds: duration });
console.log(timeAfterTrip);
Related
I've got a stopwatch app that stores the times in an array. The JS counts milliseconds from when the timer is started, then converts those milliseconds into seconds and minutes, and outputs that as a string.
What I'm trying to do is calculate the averages of these times, so I have to convert the strings into floats. It can convert a string like 10.35 to a float just fine, but it obviously doesn't work with something like 1:24.56, that's what I'm trying to fix. The time will be in one of these formats:
[seconds].[milliseconds]
[minutes]:[seonds].[milliseconds]
Any ideas? I'm really new with Javascript so I'm not sure where to go with this.
EDIT: The repo is here. All of the nonsense happens in the timer.js and main.js files.
Just split it, and add it up:
function toMS(str) {
if(!str.includes(":"))
return parseFloat(str);
const [mins, secms] = str.split(":");
const [sec, ms] = secms.split(".");
return ((+mins * 60) + +sec) * 1000 + +ms;
}
(This uses "unary plus operators" to convert the strings into numbers)
I have a strange timezone/date formatting issue that recently came up with some new code, and what makes it more strange is that it only affects two months - August and September.
The code takes a date string with UTC time formatted like this:
10-06-2017 09:29:15
And converts it to a new string with the same format but with local time. The zeroPad function ensures that the format remains the same.
We implemented it in March and everything worked fine. It's within Classic ASP on IIS9/Server 2012.
As soon as we got to August, it broke. 08-10-2017 09:33:06 becomes 12-09-2016 20:33:06.
Can anyone see what I've done wrong?
function jsConvert(dateString) {
var patterns = dateString.split(/[\-\s:]/g);
var date = new Date(parseInt(patterns[2]),
parseInt(patterns[0]) - 1,
parseInt(patterns[1]),
parseInt(patterns[3]),
parseInt(patterns[4]),
parseInt(patterns[5]));
date.setTime(date.getTime() - getTimezoneOffset() * 60 * 1000);
var result = zeroPad(date.getMonth() + 1);
result += '-' + zeroPad(date.getDate());
result += '-' + date.getFullYear();
result += ' ' + zeroPad(date.getHours());
result += ':' + zeroPad(date.getMinutes());
result += ':' + zeroPad(date.getSeconds());
return result;
}
function zeroPad(number) {
return (number < 10) ? '0' + number : number;
}
What are the units of time in your getTimezoneOffset() function?
Your code is written as though the getTimezoneOffset() function returns a number of minutes, since you are multiplying by 60 and then 1000, to get millseconds.
But if your getTimezoneOffset is returning seconds, you will be over-doing the multiplication and therefore jumping back too far in time.
I think it would have to be milliseconds, to jump back the distance you are getting. #CBroe above mentions that perhaps you mean the builtin getTimezoneOffset function, which is indeed in minutes. Perhaps you have a separate getTimezoneOffset function defined in your code elsewhere, that returns an answer in milliseconds? In which case CBroe's answer fixes it.
My next suggestion would be to add lines of debugging code
For example, could you add the following?
At the beginning, add console.log("A",dateString).
After var patterns = dateString.split(/[\-\s:]/g); add a line console.log("B",patterns);.
After var date = ...(patterns[5])); add a line console.log("C",date);.
After date.setTime...1000); add a line console.log("D",date); console.log("E",getTimezoneOffset());.
If you show us the output of these lines, we should be able to pinpoint the problem easily. I have included item E because I am just wondering if there is yet another getTimezoneOffset() function in your system, which we are not aware of, or something. Seeing its value will help reassure everyone.
Meanwhile can you confirm the time zone you are running the code in? I am guessing it is in the USA rather than Europe, from your preference for putting month before the day?
So as it turns out this is a known, albeit obscure issue. It has to do with the fact that parseInt assumes that numbers with leading zeros are NOT base 10, but instead radix. It's well documented here: Javascript parseInt() with leading zeros
Once I made the change to:
parseInt(patterns[2]**, 10**);
All was good.
Thanks for the input.
I am trying to compare the time now and a time in a future date. When these times are the same I want to show a message. However, the code below is not working. I have been checking the console and now.getTime() is never the same as end.getTime(), presumably as they are in ms?
Does anyone know how to overcome this? Following this answer here I believe it should work.
function compareTimes() {
var end = new Date("August 31, 2016 11:04:18");
var now = new Date();
if (now.getTime() == end.getTime()) {
clearInterval(timer);
document.getElementById('countup').innerHTML = 'EXPIRED!';
return;
}
}
setInterval(compareTimes, 1000);
setInterval will execute the function compareTimes every second and your function will compare the times at that very instant. It is highly unlikely that both the times will be same, hence you won't be able to set your div to EXPIRED . In order to overcome this i suggest you check if the time is greater than the current time i.e if (now.getTime() > end.getTime()) then set the state as EXPIRED in your div.
I'm trying to decrement a variable once a day. I have written the following code for that.
var counter = 10; //any value
setInterval(function() {
counter = counter - 1;
}, 86400000);
Is there a better or efficient way to achieve the same thing ?
P.S : - I do not wish to use any libraries.
The only thing I see you miss is to set the initial value of counter variable.
I would write:
var counter = 1000; // or any useful value
setInterval(function() {
--counter;
}, 24 * 60 * 60 * 1000); // this is more self-explanatory than 86400000, and, being evaluated just once, it will have a tiny effect on the performace of the script
I don't see any problem in the way you write it. You use interval, ok, but this is not the worst evil you may do to set up the variable value.
You may think of another solution with a function which returns you the current counter.
var initialValue = 20000;
function getCounter() {
return initialValue - Math.floor(Date.now() / 1000 / 60 / 60 / 24);
}
console.log(getCounter());
The difference is that it takes the current day number starting from the UNIX time beginning. Every day the day number will be increased, so the result of the function will be decreased by 1.
But still I don't see how this solution can be better than yours.
I'm not totally sure why, but using setInterval like this makes me uncomfortable.
If I were to require this, I would use something like this approach:
var counter = 10;
var timeout = new Date();
setInterval(function(){
if(new Date() >= timeout)
{
--counter; // the action to perform
timeout = new Date(timeout.getTime() + 86400000); // update the timeout to the next time you want the action performed
}
console.log(counter);
},1000); // every second is probably way more frequent than necessary for this scenario but I think is a decent default in general
One thing that this allows is to, for example, set the next timeout to midnight of tomorrow rather than being locked in to "X seconds since the previous execution". The key is the inversion of control - the action itself can now dictate when it should next run.
Though I would probably abstract away the details behind an interface accepting a start, interval, and action.
The biggest problem in my eyes is that you have to keep this one JS process running consistently for days at a time to have it do what you need. The world is not so perfect that things don't need an occasional reboot...including the average JS process.
Personally I would store a timestamp of my starting point, then (whenever I need to know how much time has elapsed) grab a new timestamp and use it to calculate how many days it has been. That way even if something interrupts my process I can still be right where I started.
Maybe use window.localStorage to save the last time, and if it is greater than 60*60*24 (seconds in a day) set the last time to this morning/now/1:00 and then decrease the value and save it.
Example:
var d = new Date();
var mins = -(1+d.getHours())*60+d.getMinutes();
var secs = mins*60+d.getSeconds(); // total seconds passed today from 1:00
var now = d.getCurrentTime():
var lastCheck = localStorage.getItem("lastCheck");
if (!lastCheck)
{
localStorage.saveItem("lastCheck",now-secs); // beginning of today
}
var dayPassed = now - lastCheck > 24*60*60; // change to see if a day has passed
if (dayPassed)
{
// save seconds
localStorage.setItem("counter",localStorage.getItem("counter")-1);
localStorage.saveItem("lastCheck",now-secs); // beginning of today
}
It makes more sense to me to check how many days have passed since a specific date and decrement that number of days from the counter. Mostly just because I wouldn't expect anybody to leave the same page open without the need or want to reload for days on end. I would do something like this:
counter = 365; // original counter
var start = new Date(2016, 03, 20); // original date
var now = new Date();
var days = Math.floor(Math.abs(start.getTime()-now.getTime())/(24*60*60*1000))
counter -= days;
That way every time you visited the page, it would be decremented correctly. Note that this ignores any issues with leap days or time zones. The example above would have a counter of 360 for me. And then if you did expect it to be open for days, reload it automatically with:
self.setTimeout(function(){document.location.reload()}, 86400000);
Thanks for opening my question. What I'm doing, to me, should be very simple. I am a beginner for programming so I am not aware of what I need to get this done. I need help.
The problem:
I have to have 4 columns for times. (Travelto, Arrive, Depart, Travelfrom) I don't always use all of them so my script has to recognize that I want certain values based on which cells in a row are blank or which have content. I have tried using isblank() on the spreadsheet to determine a binary number which I then convert to a decimal. I'd like my script to do that so I don't have to add another column to my google sheets. I think I would use an array and then check if each element is blank in the array then multiply each element in that array by 1 so it's now a number instead of a boolean. Then I want to take the elements of the array and convert them into a single binary number and convert that to a decimal number to feed to my switch case, which will contain the correct way to calculate the hours and return the hours in decimal so it should be formated such as 1.75 for 1 hr 45 mins. The value it returns must be able to be summed so the function can't return a string. also I prefer 2 decimal places for the output.
I have attempted to figure out how to calculate the time in google's apps Script. I have had limited success. The output of my script is unintelligible as to how it got the answer it did. This is probably because I can't figure out how to tell what the script sees the times as. does it see 13:00:00, 0.5416667, or something completely different? I can't seem to figure it out.
I want to pass two values from a google sheets spreadsheet, which are visually formatted as time, then take those two times subtract one from the other and get the amount of time between them, the duration so that I know how many hours have been worked.
function worked(time1,time2) //pass 2 time values to function
{ //Start of the function
var time1; //declare time1 variable
var time2; //Declare time 2 variable
var outnumber = time1-time2; //Declare outnumber and subtract time1 from time2
return outnumber //return the difference of time1 and time2
}
here's the link to my sheet and code included in the editor. anyone with the link can comment
https://docs.google.com/spreadsheet/ccc?key=0Ar4A89ZoxmJCdHBFR0VCblVtWUVvR3hFbTdlcjdKNUE&usp=sharing
Please tell me what I'm doing wrong or not doing at all to make this work.
Thanks
Goldenvocals369
The number you are seeing outputted is the difference in ms. You need to convert ms to the format you want.
I found a neat way to do that here: https://coderwall.com/p/wkdefg
Your code would look like this.
function worked(time1,time2)
{
var time1;
var time2;
var outnumber = time1-time2;
return msToTime(outnumber)
}
function msToTime(duration) {
var milliseconds = parseInt((duration%1000)/100)
, seconds = parseInt((duration/1000)%60)
, minutes = parseInt((duration/(1000*60))%60)
, hours = parseInt((duration/(1000*60*60))%24);
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
return hours + ":" + minutes + ":" + seconds;
}
You can use this function. The function's documentation is:
Returns the displayed value of the top-left cell in the range. The
value will be of type String. The displayed value takes into account
date, time and currency formatting formatting, including formats
applied automatically by the spreadsheet's locale setting. Empty cells
will return an empty string.