I have a function in my VBA code which sets a specific Date format for a textbox.
This is my code to verify the Date is in the correct format:
Function CheckDate(DateStg As String) As Boolean
If DateStg = "" Then
' Accept an empty value in case user has accidentally moved to a new row
CheckDate = True
lblMessage.Caption = ""
Exit Function
End If
If IsDate(DateStg) Then
CheckDate = True
lblMessage.Caption = ""
Else
CheckDate = False
lblMessage.Caption = "Sorry I am unable to recognise " & DateStg & " as a date."
End If
End Function
In addition to checking if the date in the textbox is an actual date, I need to verify that the textbox date is not less than the current date minus 1 month, and. Also, I would like to verify that the date is not more than the current date plus 1 year.
So:
DateStg > Today - 1 month
DateStg < Today + 1 year
Thanks for your help in advance.
You have a few functions you can use:
''Assume date is not good
DateOK=False
If IsDate(DateStg) Then
If DateStg > dateAdd("m",-1,Date()) _
And DateStg < dateAdd("m",12,Date()) Then
''Date is good
DateOK=True
End If
End if
For the most part, textboxes can be set to only accept dates and you can set validation rules to check the range, so code may not be necessary.
If you just want to check the date, you can use the DateAdd-function to get the dates to compare:
'Subtract a month from today and return it as a string
Format(DateAdd("m", -1, Now), "yyyy-mm-dd")
'Add a year to today and return it as a string
Format(DateAdd("yyyy", 1, Now), "yyyy-mm-dd")
Related
I am trying to display a message based on a date range. If it falls between the BeginDate and EndDate then the date should be display and if the date falls on 10-09-2017 then another message should be displayed, otherwise then it should display the date. This seems to fail and go directly to the else statement. My eyes are not picking up the error. How can something get displayed between a date range in VBScript.
<%
Dim DateT
Dim BeginDate
Dim EndDate
BeginDate = Day("2017-05-26")
EndDate = Day("2017-11-04")
DateT = Day(Date)
If BeginDate >= DateT =< EndDate
THEN response.write(DateT)
ElseIF BeginDate = Day("2017-10-09")THEN
response.write(DateT)
Else
response.write(DateT)
End If
%>
I believe your syntax is incorrect and your logic is overcomplicated.
Not exactly sure of the syntax here or the correct date format but I added some response.write for you to check (which should have been your first stop)
Your logic is overcomplicated because the only time it doesn't display the date is when it matches that special date..
Dim DateT
Dim BeginDate
Dim EndDate
BeginDate = CDate("2017-05-26")
EndDate = CDate("2017-11-04")
DateT = Date()
' remove these when you're finished debugging
response.write(BeginDate)
response.write(EndDate)
If BeginDate = CDate("2017-10-09")THEN
response.write("Another message")
Else
response.write(DateT)
End If
I want to use Python to parse a user input string and need to know which portion of date has been specified, e.g.,
"Jan. 2017" => Month = 1, Year = 2017
The result should tell me only month and year are specified in the string, and return those values, while:
"2003-05-01"
specifies day, month and year.
I tried to use dateutil.parser.parse, and gave it a rare default date value. e.g., 1900/01/01, and then compare the parsed result with the default date and see the difference. But if the month or day are both 1 in the parsed result,
it needs one more round of parsing with a different default value, in order to rule out the possibility of it being the default value, or from the user input.
The above way seems quirky. Is there a library for me to parse commonly used date string format and knowing what has been parsed?
I ended up doing the quirky way:
from dateutil.parser import parse
# parse the date str, and return day/month/year specified in the string.
# the value is None if the string does not have information
def parse_date(date_str):
# choose two different dates and see if two parsed results
default_date1 = datetime.datetime(1900, 1, 1, 0, 0)
default_date2 = datetime.datetime(1901, 12, 12, 0, 0)
year = None
month = None
day = None
try:
parsed_result1 = parse(date_str, default=default_date1)
parsed_result2 = parse(date_str, default=default_date2)
if parsed_result1.year == parsed_result2.year: year = parsed_result2.year
if parsed_result1.month == parsed_result2.month: month = parsed_result2.month
if parsed_result1.day == parsed_result2.day: day = parsed_result2.day
return year, month, day
except ValueError:
return None, None, None
I have to make a report that will execute automatically several times, every day, with different settings. The report selects records between two dates. Now, the client wants to be free to define, in a text or excel file, the dates that will run each report. For example, every day I want to run the report for that day, and for that same day the previous year, and for the same day the previous week, and for the previous week, and for the next month of the previous year. Then on the first of every month, the whole previous month, etc. I think you get the gist.
My question is: Is there any established way of doing this? Some text encoding for offsets of dates? I've looked and nothing appears. However, it doesn't seem an outlandish proposition. I suppose the situation has happened before. I wouldn't like to reinvent the wheel. If, however, there is nothing, any idea would be welcome :-)
OK, so I did it myself, no free lunch this time :-) The format I chose is a string like:
+1W,fW,-1D (That would be the next week, first day of that week, then one day before that)
Separator is comma, keys are D for day, W for week, M for month, Y for year, F for first, L for Last (all keys can be lower or upper case). The format ignores whitespace, but the last non-whitespace character must be a key. Only integer offsets allowed.
I give my first implementation of a VBA function that interprets a text offset. It seems to work, although little testing done yet.
Well, that's all, for posterity.
Public Function GetDateFromOffset(fecIni As Date, sDif As String) As Date
Dim sArr() As String, ii As Integer, fRes As Date
fRes = fecIni
sArr = Split(sDif, ",")
For ii = LBound(sArr()) To UBound(sArr())
fRes = ApplyOneOffset(fRes, sArr(ii))
Next ii
GetDateFromOffset = fRes
End Function
Public Function ApplyOneOffset(fecIni As Date, sDif As String) As Date
Const C_DAY As String = "D", C_WEEK As String = "W", C_MONTH As String = "M", C_YEAR As String = "Y"
Const C_FIRST As String = "F", C_LAST As String = "L"
Dim iDesp As Integer, sDesp As String, fRes As Date
sDesp = UCase(Right(sDif, 1))
sDif = Trim(UCase(Left(sDif, Len(sDif) - 1)))
Select Case sDesp
Case C_DAY
If (IsNumeric(sDif)) Then
fRes = DateAdd("d", CInt(sDif), fecIni)
End If
Case C_WEEK
If (sDif = C_FIRST) Then
fRes = dhFirstDayInWeek(fecIni)
ElseIf (sDif = C_LAST) Then
fRes = dhLastDayInWeek(fecIni)
ElseIf (IsNumeric(sDif)) Then
fRes = DateAdd("ww", CInt(sDif), fecIni)
End If
Case C_MONTH
If (sDif = C_FIRST) Then
fRes = dhFirstDayInMonth(fecIni)
ElseIf (sDif = C_LAST) Then
fRes = dhLastDayInMonth(fecIni)
ElseIf (IsNumeric(sDif)) Then
fRes = DateAdd("m", CInt(sDif), fecIni)
End If
Case C_YEAR
If (sDif = C_FIRST) Then
fRes = DateSerial(YEAR(fecIni), 1, 1)
ElseIf (sDif = C_LAST) Then
fRes = DateSerial(YEAR(fecIni), 31, 12)
ElseIf (IsNumeric(sDif)) Then
fRes = DateAdd("yyyy", CInt(sDif), fecIni)
End If
Case Else
fRes = fecIni
End Select
ApplyOneOffset = fRes
End Function
I'm relatively new to VB6 and I've just been given an assignment where I have a date - for example '4/12/2016' - from this date, i'm trying to find out the day that it is. So let's say it's a wednesday. Now from this day, I'm trying to determine the dates for the week [sun(startdate) - sat(enddate)). How would I go about doing something like this?
EDIT: I have a pretty good idea about finding out the date for sunday and saturday, since I can simply do something along the lines...
dim dateStart,dateend as date
Ex of date given to me = '4/12/2016'
Dim dateDay as variant
dateDay = whatever I get here - i'm assuming that a date will return a number for whatever day it is ???? Not sure
Select Case dateDay
case 1 -Monday?
dateStart=dateadd("d",-1,'4/12/2016)
dateEnd = dateadd("d",6, '4/12/2016)
case 2 -Tuesday?
datestart = dateadd("d",-2,'4/12/2016)
dateend = dateadd("d",5,'4/12/2016)
End Select
Basically do the SELECT statement for all cases. Am I on the right track?
This code:
Debug.Print Format(DatePart("w", Now), "dddd")
will print whatever day of the week it is now to the Immediate window. If you want the abbreviated day of week, use "ddd" for the format.
Now, this code:
Dim DOW As String
Select Case DatePart("w", Now)
Case vbSunday
DOW = "Sunday"
Case vbMonday
DOW = "Monday"
Case vbTuesday
DOW = "Tuesday"
Case vbWednesday
DOW = "Wednesday"
Case vbThursday
DOW = "Thursday"
Case vbFriday
DOW = "Friday"
Case vbSaturday
DOW = "Saturday"
End Select
Debug.Print DOW
will do the same thing. However, it shows you how to evaluate programmatically which day of the week you're dealing with, by using vbSunday, vbMonday, etc. That should give you what you need to get started on your Select statement. To use your example, DatePart("w", "4/12/2016") evaluates to 3, or vbTuesday.
VB6 reference documentation is here, and rather well hidden I might add. Look up Format and DatePart to get familiar with other options.
EDIT: As MarkL points out, the Weekday function is available in VB6 (I thought it wasn't), and is simpler (one less argument) than using DatePart. This code:
Debug.Print Format(Weekday(Now), "dddd")
will also print whatever day of the week it is to the immediate window. jac has also provided a link to the Weekday function in the comments above.
You can try below codes, The code will return name of the day.
txtDateTime.Text = WeekdayName(Weekday(Now))
txtDateTime.Text = WeekdayName(Weekday(12 / 30 / 1995))
txtDateTime.Text = WeekdayName(Weekday(Date))
How to show reminder by month?
I have created this partial code and I am stucked how to manipulate to check and get reminder by month.
reminders = ScheduledActionService.GetActions<Reminder>();
if (reminders.Count<Reminder>() > 0)
{
}
else
{
}
If there is reminder I want to check the month and get the month i want.
try this one:
var month = 5; // your month
var remindersOfMonth = ScheduledActionService.GetActions<ScheduledAction>()
.Where(a=> a.BeginTime.Month == month);
According to this when you use GetActions() it will return you a list of
ScheduledAction. Now according to this ScheduledAction has a property called BeginTime which is of type DateTime so it has Month property which is your interest.
variable "a" in the statement is in fact each of ScheduledAction in the collection which are begin evaluated again the month.