i have 2 date picker
Dim pday, eitday, otherday, tpenalty, difday, subpenalty As Integer
difday = Val(L1.Caption) - Val(L2.Caption)
pday = 7
eitday = 8
otherday = difday - eitday
tpenalty = 25
If difday <= pday Then
PENALTY.Caption = 0
ElseIf difday = eitday Then
PENALTY.Caption = tpenalty
ElseIf difday > eitday Then
For i = 0 To otherday - 1
subpenalty = subpenalty + 5
Next i
PENALTY.Caption = tpenalty + subpenalty
End If
the problem is when the month is change the calculation is invalid.
I'm guessing based on your code (as many things are unclear), but this should give the number of days between two dates:
difday = DateDiff("d", StartDate, EndDate)
I've used StartDate and EndDate to signify the start and end of the lone period which are used to set L1 and L2, as you shouldn't be converting from strings to dates for calculations.
Related
I have the vb net code for first and last day of the Quarter, but I am trying to do this for first and last day of Bi monthly period ( which end on these months Feb, Apr, Jun, Aug, Oct, Dec)
So the bimonthly start and end dates are
Jan 1 to Feb 28 (or 29)
Mar 1 to Apr 30,
May 1 to Jun 30,
etc
Any ideas how to do this? (in Vb thanks)
'To get the first day of the quarter
Public Shared Function xFirstDayOfQuarter(ByVal theDay As DateTime) As DateTime
Dim currQuarter As Integer = (Month(theDay) - 1) \ 3 + 1
Dim FirstDayQuarter As DateTime = DateSerial(Year(theDay), 3 * currQuarter - 2, 1)
Return FirstDayQuarter
End Function
'To get the last day of the quarter
Public Shared Function xLastDayOfQuarter(ByVal theDay As DateTime) As DateTime
Dim currQuarter As Integer = (Month(theDay) - 1) \ 3 + 1
Dim LastDayQuarter As DateTime = DateSerial(Year(theDay), 3 * currQuarter + 1, 0)
Return LastDayQuarter
End Function
This how far I got ..
Private Function DayOfBiMonth(ByVal theDay As DateTime, ByVal FirstOrLast As Integer) As DateTime
Dim res As DateTime = Util.gBASEDATE
Dim Mth As Integer = Month(theDay)
Dim Yr As Integer = Year(theDay)
Dim BiMonth As Integer = -1
Select Case Mth
Case Is = 1, 2
If FirstOrLast = 1 Then
BiMonth = 1
ElseIf FirstOrLast = 2 Then
BiMonth = 2
End If
Case Is = 3, 4
If FirstOrLast = 1 Then
BiMonth = 3
ElseIf FirstOrLast = 2 Then
BiMonth = 4
End If
Case Is = 5, 6
If FirstOrLast = 1 Then
BiMonth = 5
ElseIf FirstOrLast = 2 Then
BiMonth = 6
End If
Case Is = 7, 8
If FirstOrLast = 1 Then
BiMonth = 7
ElseIf FirstOrLast = 2 Then
BiMonth = 8
End If
Case Is = 9, 10
If FirstOrLast = 1 Then
BiMonth = 9
ElseIf FirstOrLast = 2 Then
BiMonth = 10
End If
Case Is = 11, 12
If FirstOrLast = 1 Then
BiMonth = 11
ElseIf FirstOrLast = 2 Then
BiMonth = 12
End If
End Select
If FirstOrLast = 1 Then
res = DateSerial(Yr, BiMonth, 1)
ElseIf FirstOrLast = 2 Then
res = DateSerial(Yr, BiMonth, 20)
res = Util.LastDayOfMonth(res)
End If
Return res
End Function
Private Function LastDayOfMonth(ByVal theDay As DateTime) As DateTime
Dim FirstDayMonth As DateTime = DateSerial(Year(theDay), Month(theDay), 1)
Dim LastDayMonth As DateTime = FirstDayMonth.AddMonths(1).AddDays(-1)
Return LastDayMonth
End Function
This works best
'To get last day of BIMonth 2,4,6,8,12
Public Shared Function LastDayOfBiMonth(ByVal theDay As DateTime) As DateTime
Dim FirstDayBiMonth As DateTime = FirstDayOfBiMonth(theDay)
Dim LastDayBiMonth As DateTime = FirstDayBiMonth.AddMonths(2).AddDays(-1)
Return LastDayBiMonth
End Function
'To get first day of BIMonth 1,3,5,7,9,11
Public Shared Function FirstDayOfBiMonth(ByVal theDay As DateTime) As DateTime
Dim Mnth As Integer = Month(theDay)
If Mnth Mod 2 = 0 Then
Mnth = Mnth - 1
End If
Dim FirstDayBiMonth As DateTime = DateSerial(Year(theDay), Mnth, 1)
Return FirstDayBiMonth
End Function
In Matlab, I want to create a plot of the hourly prices "DataSeriesEl" (size is 744 x 1). They should go from January 1, 2008, 00:00:00 to January 31, 2008, 23:00:00. However, my code switches at January 7 to 05:59:59 - see below. Do you know what the problem is?
StartYearData = 2008;
StartMonthData = 1;
StartDayData = 1;
date(1) = datenum(StartYearData,StartMonthData,StartDayData,0,0,0);
for m = 2:length(DataSeriesEl)
date(m) = addtodate(date(m-1), 1, 'hour');
end
str = datestr(date)
I think this is a much simpler solution if you use this method , for this method you dont have to use a for loop
StartYearData = 2008;
StartMonthData = 1;
StartDayData = 1;
EndDayData = 31;
dates(1) = datenum(StartYearData,StartMonthData,StartDayData,0,0,0);
dates(2) = datenum(StartYearData,StartMonthData,EndDayData ,23,0,0);
myDateTime = datetime(dates, 'ConvertFrom', 'datenum')
hours = (myDateTime (2) - myDateTime (1) )/duration(1,0,0);
date = linspace(myDateTime(1),myDateTime(2),hours +1 )
I want to display the weeks dates based on a week number that I get from my db.
So if I get week=43 then it would display all 7 dates for that week.
Like this.
M=23
T=24
W=25
T=26
F=27
S=28
S=29
Have tested with a lot of date formating but I can't get it working.
So any input really appreciated, thanks!
I ended up with this, works perfect!
currentDate = Date
weekNumber=DatePart("ww", currentDate, vbMonday, vbFirstFourDays)
y = Year(Date)
Public Function FirstDayOfWeek(Year, Week)
Dim TempDate
TempDate = DateSerial(Year, 1, 1)
Do Until DatePart("ww", TempDate, vbMonday, vbFirstFourDays) = 1
TempDate = TempDate + 7
Loop
TempDate = TempDate + (7 * (Week - 1))
FirstDayOfWeek = TempDate - Weekday(TempDate, vbMonday) + 1
End Function
Dim startDatum
Dim slutDatum
startDatum = FirstDayOfWeek(y, weekNumber)
slutDatum = startDatum + 6
mon=DatePart("d", startDatum)
tus=DatePart("d", startDatum+1)
wen=DatePart("d", startDatum+2)
tur=DatePart("d", startDatum+3)
fri=DatePart("d", startDatum+4)
sat=DatePart("d", startDatum+5)
sun=DatePart("d", startDatum+6)
Well, there isn't really an inverse DatePart() function, so you have to make your own calculations.
dim w, wd, y, m, i
y = Year(Date) '- year of the week in question; I'm using today's date
w = 43
wd = DateAdd("d",w*7,CDate("1/1/" & y)) '- adjust as needed for 1st week of year
m = DateAdd("d",2-Weekday(wd),wd) '- find Monday of week
Hopefully, you can go from there.
How can I get the Today -2 days (the last 2 working days from now)? but skipping the weekend?
Example #1: Today is February 25, I want February 21
Example #2: Today is February 26, I want February 24
PS: Date format is DD/MM/YYYY
I have this, but the result is going forward, should I use datediff or what?:
<%
Dim d
d = DateAdd("m", 1, Now)
d = "01/" & Month(d) & "/" & Year(d)
d = DateAdd("d", -1, d)
If Weekday(d) = 7 Then
d = DateAdd("d", -1, d)
ElseIf Weekday(d) = 1 Then
d = DateAdd("d", -2, d)
End If
Response.Write "Day: " & d
%>
To get your desired result you need to subtract 3 days on Saturdays, 4 days on Sundays and Mondays, and 2 days on all other days. This can be achieved with something like this:
today = Now
num = Weekday(today, vbWednesday)
d = today - (2 + num\5 + num\6)
response.write "Two working days back: " & d
The Weekday function returns a numeric value for each weekday. By basing the week on Wednesday you can calculate the additional number of days you need to subtract from the current date with integer divisions:
num\5 returns 1 for Saturday, Sunday and Monday, and 0 otherwise.
num\6 returns 1 for Sunday and Monday, and 0 otherwise.
Thus the term 2 + num\5 + num\6 becomes 3 for Saturdays, 4 for Sundays and Mondays, and 2 for all other days.
This might be overkill for what you need but here are two routines I use in my scripts to add or subtract workdays while considering weekends and holidays.
Function AddWorkingDays(dtStart, intDays)
' Start/Default case...
AddWorkingDays = CDate(dtStart)
' If positive days, step forward, otherwise step backward...
Dim intStep, intCount
If intDays > 0 Then intStep = 1 Else intStep = -1
Do While intCount <> intDays
AddWorkingDays = AddWorkingDays + intStep
If IsValidDate(AddWorkingDays) Then intCount = intCount + intStep
Loop
End Function
Function IsValidDate(d)
Dim intWeekday, intMonth, intDay
intWeekday = Weekday(d)
intMonth = Month(d)
intDay = Day(d)
' Weekend dates are not acceptable...
If intWeekday = vbSaturday Or intWeekday = vbSunday Then Exit Function
' Holidays are also not acceptable...
If intMonth = 01 Then If intDay = 01 Then Exit Function ' New Year's Day
If intMonth = 07 Then If intDay = 04 Then Exit Function ' Independence Day
If intMonth = 12 Then If intDay = 25 Then Exit Function ' Christmas Day
' Memorial Day is the last Monday in May...
If intWeekday = vbMonday Then If intMonth = 05 Then If intDay >= 25 Then Exit Function
' ... (Thanksgiving, others) ...
' All tests passed. Date is a valid workday...
IsValidDate = True
End Function
I am trying to calculate the time difference between 2 date time strings.
I have 2 inputs where the input string is something like this "1:00 PM" and the second one "3:15 PM". I want to know the time difference. So for the above example I want to display 3.15
What I have done:
Converted the time to a 24 hours format. So "1:00 PM" becomes "13:00:00"
Appended the new time to a date like so: new Date("1970-1-1 13:00:00")
Calculated the difference like so:
Code:
var total = Math.round(((new Date("1970-1-1 " + end_time) -
new Date("1970-1-1 " + start_time) ) / 1000 / 3600) , 2 )
But the total is always returning integers and not decimals, so the difference between "1:00 PM" and "3:15 PM" is 2 not 2.15.
I have also tried this (using jQuery, but that is irrelevant):
$('#to_ad,#from_ad').change(function(){
$('#total_ad').val( getDiffTime() );
});
function fixTimeString(time){
var hours = Number(time.match(/^(\d+)/)[1]);
var minutes = Number(time.match(/:(\d+)/)[1]);
var AMPM = time.match(/\s(.*)$/)[1];
if(AMPM == "PM" && hours<12) hours = hours+12;
if(AMPM == "AM" && hours==12) hours = hours-12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours<10) sHours = "0" + sHours;
if(minutes<10) sMinutes = "0" + sMinutes;
return sHours + ':' + sMinutes + ':00';
}
function getDiffTime(){
var start_time = fixTimeString($('#from_ad').val());
var end_time = fixTimeString($('#to_ad').val());
var start = new Date("1970-1-1 " + end_time).getTime(),
end = new Date("1970-1-1 " + start_time).getTime();
return parseInt(((start - end) / 1000 / 3600, 10)*100) / 100;
}
But the total_ad input is displaying only integer values.
How can I fix this problem?
Math.round rounds to the nearest integer, multiply and divide instead
var start = new Date("1970-1-1 " + start_time).getTime(),
end = new Date("1970-1-1 " + end_time).getTime();
var total = (parseInt(((start-end) / 1000 / 3600)*100, 10)) / 100;
FIDDLE
When you take the time 15:15:00 and subtract 13:00:00, you're left with 2.15 hours, not 3.15, and this example would return 2.15 even without making sure there is only two decimals, but for other times that might not be the case.
You could also use toFixed(2), but that would leave you with 3.00 and not 3 etc.
This is how I calculate it:
calculateDiff();
function calculateDiff(){
_start = "7:00 AM";
_end = "1:00 PM";
_start_time = parseAMDate(_start);
_end_time = parseAMDate(_end);
if (_end_time < _start_time){
_end_time = parseAMDate(_end,1);
}
var difference= _end_time - _start_time;
var hours = Math.floor(difference / 36e5),
minutes = Math.floor(difference % 36e5 / 60000);
if (parseInt(hours) >= 0 ){
if (minutes == 0){
minutes = "00";
}
alert(hours+":"+minutes);
}
}
function parseAMDate(input, next_day) {
var dateReg = /(\d{1,2}):(\d{2})\s*(AM|PM)/;
var hour, minute, result = dateReg.exec(input);
if (result) {
hour = +result[1];
minute = +result[2];
if (result[3] === 'PM' && hour !== 12) {
hour += 12;
}
}
if (!next_day) {
return new Date(1970, 01, 01, hour, minute).getTime();
}else{
return new Date(1970, 01, 02, hour, minute).getTime();
}
}