Trying to create Javascript form to calculate end times - javascript

I'm trying to create a form that users can fill out to calculate a projected project end time (think time punches).
For example:
You're projected to spend 8 hours on a project, you start that project at 8 am and you'll have 1 hour of downtime that doesn't count towards project time in the middle.
8am start + 8 hour projection + 1 hour delay = Projected End Time of 5pm.
I'm a novice at best with Javascript though I can typically find a similar script online and cannibalize then rewrite it for my needs. In this instance though, I feel like I'm out of my depth by a long shot, as the closest I could get was a script that adds 2 boxes together but doesn't account for time (so 8+9=17, not 5 like I needed).
Any help would be greatly appreciated! If need be I can paste the script I was trying to edit, though I'm pretty sure it's useless. I tried to Google how to write this basic thing I want to use but after 3 hours and no progress I decided to cave in and ask.
Thanks for taking the time to read!

I am not sure about your overall calculation, but if you want to account for AM/PM time in your result you simply subtract 12 hours from the result. 17-12 = 5 PM.
Here is a rough sample...
var time = 17;
AmPmTime(17); // 5 PM
function AmPmTime(t) {
var result = t + " AM";
if (t > 12) {
result = (t-12) + " PM";
} else if (t == 0)
result = "12 AM";
}
return result;
}

Related

How to add time to time (minutes + minutes) in Javascript ? (Not to Date)

I don't finde anywhere how to add time to time, like adding 45 minutes to 45 minutes and having 1:30 (1 hour, 30 minutes).
I just find how to add time to actual Date.
Heres an example in PHP of what I'm looking for:
$seconds_toadd = 45;// VALUE TO GET FROM A TEXTBOX
$actual_value = '00:45'; //45 minutes, 0 hours is the actual value
$resultado = new DateTime($lectura_xml);
$resultado->add(new DateInterval('PT' . $seconds_toadd . 'S'));//This is how I add seconds in PHP
$stamp = $resultado->format('I:s');//Formatting result
echo $stamp;
Thank you.
Moment.js is a great library for date/time handling in JavaScript and better than the standard API and any homemade solution in many aspects.
Very simple but similar use case from their docs:
var a = moment.duration(1, 'd');
var b = moment.duration(2, 'd');
a.add(b).days(); // 3

Add a defined amount to a value each day in JavaScript

I've a problem:
I have a website which should display a number in a specific div.
Now I want the number to increase by a specific amount every day, I am only allowed to use JavaScript and it should return the final number.
For example:
Today the number (start) is 100. Tomorrow it should be 110, the next day 120,... so the function should add 10 to the number each day.
I thought of a kind of loop but this would only work when I reload the site and wait a day :P Is there another option?
Thanks!
You may need to calibrate this a bit, but this should get you going
var startDate = new Date('2-10-2016');
var today = new Date();
var diff = Math.floor((today - startDate)/(1000*60*60*24))
var answer = 100 + diff * 10
If you know the number N that should be added each day, you can actually implement do, in pseudocode:
numberOfPassedDays = Today - initDay
resultToShow = N * numberOfPassedDays + numberAtInit
and calculate it every refresh of the page (in your example N=10 )

Determine when it's monday by the minute

I feel this is a simple question, but there are several factors in play here which make it a bit more complex than it would seem.
For a site, we need to change a page weekly (with javascript/jQuery, no PHP or anything), with the transition being sunday 00:00 AM (from sunday to monday). It is important this happens on that minute/hour, due to page content being relevant for past and next weeks (actions, discounts, product advertisements etc.).
I searched a lot, tried several scripts, and i eventually ended up with this script:
Date.prototype.getWeek = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
};
var today = new Date();
var currentWeekNumber = today.getWeek();
//Returns weeknumber, content is adjusted through if(weeknumber == 35){etc.}
Now this seems to do what i want it to do, but when we tested it last sunday it seemed to initiate (at least) 3 hours too early as far as we could see (oddly enough not too many testers at sundaynight!).
Could anyone help me get underway with a proper script?
Something that adds to my struggle is timezones. I live in Amsterdam (GMT+2), and the visitors will be in the same timezone (Netherlands, the site is not aimed at people outside this zone).
Another thing that adds to the complexity for me, is that i am unable to test this more than 1 time a week. So any help with that would already be handy.
Summary: I need to change webcontent every week at sunday->monday 00:00 AM and a script to help me do that.
Something like this?
var d = new Date()
var n = d.getDay() //returns 0-6 for Sunday to Saturday
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
var time = hours+':'+minutes+':'+seconds;
if(n==1 && time=='00:00:00'){
//do something
}

Script for business hours javascript

I need script that will display "Open" & "Close" function on my site.
Script should display "OPEN" every day from Monday to Friday from 08:00am to 19:30pm
and for Saturday should display "OPEN" from 08:00am to 15:00pm (else display CLOSED)
Sunday is CLOSED all day long.
I try to manage this script but I was not able to achieve it:
var Digital=new Date()
var hours=Digital.getHours()
if (hours>=08:00&&hours<=19:30)
document.write('Open')
else if (hours>=19:31&&hours<=07:59)
document.write('Close')
but i need addition for the days, this is just for time.
The hours variable will be an integer number, you need to compare it to a number, like this:
if (hours >= 8 && hours <= 19)
document.write('Open')
else if (hours >= 19 && hours <= 7)
document.write('Close')
When rewrite those methods, you will need to get and compare the minutes from the Digital variable too.
You need to check the current date using if statements before checking the time. Your formatting was slightly off, as digital.getHours() returns a whole number rather than those formatted strings.
I also added a setInterval to update the status every minute, in case the page is left open for prolonged time.
Here is the fiddle: http://jsfiddle.net/u6bwJ/1/
EDIT: Fixed a few bugs (namely typos). I also see you need localization for this. I made some changes to the top of the code which adjust for timezone, so it's always displaying information based on local time. There is one caveat though, and that is that it is currently hardcoded to include daylight savings. This means it will be inaccurate once DST switches.
Line 10:
utc1Time=new Date(localTime.getTime() + (localTime.getTimezoneOffset() + 120) * 60000);
That + 120 is adding 2 hours after converting client time to UTC time, which makes it UTC+1 and then adds the DST offset. You will need to add some way to check if DST is in effect, something along the lines of
utc1Time.toString().match(/daylight/i)
but I will leave that to you, as this is probably enough of a framework for you to build upon.
Hope this helped :D

Math Functions that turns decimal value from Months into Days/Weeks

So i have a really complex system i wrote for my teacher it does a bunch of math that he had in a spreadsheet that finds out basically how long it takes someone to save up enough money to cover an asset of there. Long story short i currently have it saying for example "You will receive enough money to cover this asset in 0.38 months"
I would like to be as user friendly as possible, i dont care if i have to come up with a math function for each month. My question is, how would i spit out 0.39 Month to Weeks and Possibly if i can Days (which would actually make more sense because i can make a script that says if greater then 7 then that is equal to 1 week) to make it even more precise. I know for starters i need to know how many days are in each month, not every month is the same as we all know. Any starting point after that would be great. Im jQuery/JS for this math functions to this application.
To put my comments in answer form:
function getDays(numMonth, month, year) {
var numDays = new Date(year, month, 0).getDate();
return numMonth * numDays;
}
function getWeeks(numMonth, month, year) {
var days = getDays(numMonth, month, year);
return Math.floor(days / 7);
}
This is where numMonth = 0.39, etc.

Categories

Resources