I am stuck with one condition where I have to change the path based on time.
I will have two date variables, CheckInStartDate and CheckInEndDate, which will be coming from API.
Current time is system date time.
Path has to change in two conditions.
CheckInStartDate minus 1 hour of current time
CheckInEndDate plus 1 hour of current time.
This is the code I currently have.
$scope.checkIn = function() {
var ONE_HOUR = 60 * 60 * 1000; /* ms */
$scope.checkInStartDate= 01/16/2017 09:06:00 AM;
$scope.checkInEndDate= 01/16/2017 11:06:00 AM;
var checkInStartDate=$scope.checkInStartDate;
var checkInEndDate=$scope.checkInEndDate;
var currentDate = new Date();
var checkinStartDate=new Date(checkInStartDate);
var checkinEndDate = new Date(checkInEndDate);
if ((checkinStartDate.getTime()) > (currentDate.getTime() - ONE_HOUR) ||
(checkinEndDate.getTime()) < (currentDate.getTime() + ONE_HOUR)) {
$location.path('/checkIn')
}
else{
alert("cannot checkin");
}
}
If I got your question correctly, you want to allow check-in in the time period between the following two times:
CheckInStartDate minus 1 hour
CheckInEndDate plus 1 hour
Let's call the first point s and the second point e. The current time is c. In this case your condition should be true when
s < c < e
In JavaScript this is equivalent:
var s = new Date($scope.checkInStartDate).getTime() - ONE_HOUR;
var e = new Date($scope.checkInEndDate).getTime() + ONE_HOUR;
var c = new Date().getTime();
if (s < c && c < e) {
// check-in allowed
} else {
// not allowed
}
Related
My scheduled script sets a field to store an accrued late fee charge for each day an invoice is overdue. I am comparing the current system time against due date to work out the number of days overdue. However, I didn't take into consideration to exclude the weekend. How can I use my existing code to do this?
var current_date = nlapiStringToDate(nlapiDateToString(new Date()));
var dd = invoice.getFieldValue('duedate');
var due_date = nlapiStringToDate(dd);
if (due_date < current_date) {
//Other Calculations
var days_overdue = DateOverdue(current_date, due_date);
}
function DateOverdue(current_date, due_date) {
var time_difference = Math.abs(due_date.getTime() - current_date.getTime());
var no_days_overdue_by = Math.ceil(time_difference / (1000 * 3600 * 24));
return no_days_overdue_by;
}
The following works. Note the extra dates are to clear issues from comparing time stamps without hours, minutes and seconds. Not strictly needed for the current_date given how you are generating it but it makes a more general function.
NOTE: I don't believe you should be able to compare dates with d1 < d2.
function daysOverdue(currentDate, dueDate){
var days = 0;
var due = new Date(dueDate.getFullYear(), dueDate.getMonth(), dueDate.getDate(), 0, 0, 0);
var fromTS = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), 0, 0, 0).getTime();
if(due.getTime() >= fromTS) return 0; // not overdue
while(due.getTime() < fromTS){
if(due.getDay() !== 0 && due.getDay() != 6) days++;
due.setDate(due.getDate() + 1);
}
return days;
}
I'm working on a calendar using PHP to get my dates from a server and Javascript to do the front end math. From the server I'm using the following values to work with: Today's date, number of days in current month, and today's position in the week with reference to a Sun to Sat 0-6 convention. In order to find the first day of a given month, I have to work backwards from the current day which has known values.
The chart below shows what I'm trying to do with Javascript. A switch is when I change the pos variable back to 6(7 due to for loop problem) when I've hit 0 as I'm subtracting away from pos.
The problem is that when the for loop starts, the position of today's current day which is 5, is immediately subtracted by 1, when it shouldn't be until the next day which is 28 (previous).
By inserting delays (see the initialPos variable), I'm able to get the right value. This doesn't seem right, what is the right way to do this?
Here's a comparison of the outputs with and without delays.
With delay:
Without delay:
(without delay means I didn't add 1 to todayPositionWeek, and 7 is 6 for the reset pos counter)
<script>
today = 29;
todayPositionWeek = 5;
// start the count with todayPositionWeek shifted by 1 to not count
var initialPos = todayPositionWeek+1;
var numSwitches = 0;
var loopRan = 0;
for (var i = today; i > 0; i--) {
loopRan++;
// initialPos is 6
initialPos--;
// after it is 5 on first run
if(initialPos == 0) {
// reset pos counter
initialPos = 7;
// initially this was set to 6 but skips to 5
numSwitches++;
}
firstDayPos = initialPos;
}
alert('loop ended ' + firstDayPos + ' ' + numSwitches + ' ' + loopRan);
</script>
Today it's fine, but if the day is off(today is not on Friday), the shifted days are off...
var today = 27;
var todayPositionWeek=3;
var offset = (toDayPositionWeek+7-(today%7))%7 // ==4 because day0 ==4
function posOfAnyDay(d) {
return (offset + d) % 7;
}
function slotOfAnyDay(d) {
return Math.floor((offset + d) / 7);
}
// posOfAnyDay(13) -> 3
// slotOfAnyDay(13) -> 2
for (var i = today; i > 0; i--) {
pos = posOfAnyDay(i);
slot = slotOfAnyDay(i);
}
I am new to javascript and am working on a fallible PDF form and trying to set it to do multiple things. The first is I need to compare a user entered date to the current date to see if it is withing 5 years to the day. The second thing I need it to do is to change a fields background color if that date is at the 5 year time or outside of that range. This is the code I have been trying but it hasn't worked so far. There are 37 fields that need to be checked by this.
for(i=1;i<38;i++){
var Y = d.getFullYear();
var d = new Date();
var M = d.getMonth();
var d = new Date();
var D = d.getDay(); //n =2
var strDate = this.getField("Text"+[i]).value;
var arrDate = strDate.split('/');
var month = arrDate[0];
var day = arrDate[1];
var year = arrDate[2];
if(year+5>=Y){
if(M<=month){
if(D<=day){
this.getField("Text[i]").fillColor=color.red;
}}}}
I have updated this, it working now, can you try this now ?
for(i=1;i<38;i++)
{
var todayDate = new Date();
var strDate = "12/25/2009";
var arrDate = strDate.split('/');
var month = arrDate[0];
var day = arrDate[1];
var year = parseInt(arrDate[2]) + 5;
var userEnteredDate = new Date(year, month, day);
if(userEnteredDate <= todayDate)
{
//Color change code here...
}
}
The simplest approach so far as I know, is to instantiate a Date that is five years ago based on current time:
var t = new Date()
t.setFullYear(t.getFullYear() - 5) // t is five years ago
Then you just need to substract the user input date with this one, and see if the result is positive or negative:
var ut = new Date("......") // the Date instance from user input
if(ut - t >= 0) {
// within 5 years
} else {
// more than 5 years ago
}
The reason you can do so, is because when you substract two Date instances one another, they will be internally converted to timestamps. The less a timestamp is, the earlier the time is. So the result of substraction (a number) represents the time in between, in milliseconds.
If you don't care how long in between, you could just compare them:
var ut = new Date("......") // the Date instance from user input
if(ut >= t) {
// within 5 years
} else {
// more than 5 years ago
}
Try this
var d = new Date(),
Y = d.getFullYear(),
M = d.getMonth() + 1, // since this returns 0 - 11
D = d.getDay() + 1, // since this returns 0 - 30
strDate,
arrDate,
month,
day,
year;
for(var i = 1; i < 38; i++) {
strDate = this.getField("Text" + i).value;
arrDate = strDate.split('/');
month = parseInt(arrDate[0], 10);
day = parseInt(arrDate[1], 10);
year = parseInt(arrDate[2], 10);
if (((Y + 5) * 12 + M < year * 12 + month) || ((Y + 5) * 12 + M === year * 12 + month && D < day)) {
this.getField("Text" + i).fillColor = color.red;
}
}
I have 2 times for example: 10:00 and 1:00 now i want to check if current time... is between these 2 times in javascript.
The problem is that the closing time in this case is a next day so its before the openingstime. How can i do this the proper way for some reason i can not get around this.
i hav efound that this could solve it:
var start = new Date(2012,6,20,13).getTime();
var now = new Date().getTime();
var end = new Date(2012,6,21,2).getTime();
if( (start < now ) && (now < end )) {
console.log("opened");
}
else {
console.log("closed");
}
but how can i do it with 2 string formats like 10:00 and 2:00 because i do not see a option to put a time alone
var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
You could use a simple function like this to convert your time to a number of minutes since 0:00:
function getMinutes(str) {
var time = str.split(':');
return time[0]*60+time[1]*1;
}
And a similar function to get the current time into the same form in order to compare:
function getMinutesNow() {
var timeNow = new Date();
return timeNow.getHours()*60+timeNow.getMinutes();
}
Then convert both opening and closing time and, if it happens that closing time is before opening time, add 24 hours to it.
var now = getMinutesNow();
var start = getMinutes('10:00');
var end = getMinutes('2:00');
if (start > end) end += getMinutes('24:00');
if ((now > start) && (now < end)) { // your code here
This is the solution I've gotten to after a bit of fiddling. At the current time of 3:24 am, it outputs the correct information. changing the now array to be [13,00] also gave the correct result of 'closed' Give it a test run through to make sure it works correctly.
Edit
jQuery included solely because I am brain dead.
Edit#2
I noticed now (9pm my time) that my conversion wasn't working, it was saying 'closed', when it shouldn't have. So far, this works for any and all numbers I've put in it to test.
var start_time = [20,00]
var end_time = [12,00]
//We've got the two start times as an array of hours/minutes values.
var dateObj = new Date(); //I just feel dirty making multiple calls to new Date().etc
var now = [dateObj.getHours(),dateObj.getMinutes()]; //Gets the current Hours/Minutes
if(end_time[0] < start_time[0] && now[0] < start_time[0]){
start_time[0] -= 24; //This is something I came up with because I do a lot of math.
}else if(start_time[0] > end_time[0]){
end_time[0]+=24;
}
var el=$('#result');
var start_string = to_hms_string(start_time); //the start string converted to a string format. Made comparisons easier.
var end_string = to_hms_string(end_time); //See Above
var now_string = to_hms_string(now); //Above
console.log(start_string, now_string, end_string);
var status = (start_string < now_string && now_string < end_string) ? "Open" : "Closed";
el.html(status);
//Function to_hms_string stands for "hour-minute-second" string. First name that came up.
function to_hms_string(timearr){
var minutes = 60+timearr[1];
var hours = "";
if(Math.abs(timearr[0]) < 10){
hours = "0";
}
hours = (timearr[0]<0) ? "-"+hours+Math.abs(timearr[0]) : hours+timearr[0];
return hours+":"+minutes;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result">
PlaceHolder
</div>
You can do this, get current time. Then define you start time and end time based on the current time getting the year, month, date for tomorrow's date add 1 to the start's date see code below. Then you can compare the time the same fi condition you have. Good luck
var now = new Date();
var start = new Date(now.getFullYear(),now.getMonth(),now.getDate(),7).getTime();
var end = new Date(now.getFullYear(),now.getMonth(),now.getDate() + 1,2).getTime();
now = now.getTime();
if( now >= start && now < end) {
console.log("opened");
}
else {
console.log("closed");
}
***EDIT**
You can convert the current time to millis after you get the year, month and date. Then use your current if condition.
Jhecht This thing right here:
if(end_time[0] < start_time[0] && now[0] < start_time[0]){
start_time[0] -= 24;
}else if(start_time[0] > end_time[0]){
end_time[0]+=24;
}
it's brilliant. It works and this is the correct answer. Great job!
Here is the code I'm working with
var d = new Date(), // New Date object
M = d.getMonth(), // Month
D = d.getDate(), // Day of the month
h = d.getUTCHours(), // Hours in 24 hour time
m = d.getUTCMinutes(); // Minutes
console.log(M+'/'+D+' '+h+':'+m);
var href = location.href;
if(M == 1 && D == 13 && h >= 21 && m >= 17){
// It is time so lets just go there
window.location = href+'live';
}else{
// It isn't already time so lets check every 30 seconds
setInterval(checkTime, 1000)
}
function checkTime() {
if(M == 1 && D == 13 && h >= 21 && m >= 17){
// It is time so lets just go there
window.location = href+'live';
}
console.log('checked time');
}
I'm trying to check the date and time and if it's the correct date and time, forward to a different page, if it's not, then check every few seconds (every 1 second for now but I'll probably bump this up to 15 or 30) and check again and if it is now the correct date and time then forward to the new page.
The first if statement works but it doesn't seem to be running the if statement inside of the set interval function.
Maybe I just don't understand how setInterval works totally but I can't see a problem with me code.
Because you are NOT updating the variables, the values never change.
You need to do the date object check every time. They do not update.
The following
d = new Date(), // New Date object
M = d.getMonth(), // Month
D = d.getDate(), // Day of the month
h = d.getUTCHours(), // Hours in 24 hour time
m = d.getUTCMinutes(); // Minutes
needs to be in your checkTime method.