Comparing Dates With User Input - date

I have found a statement that compares two dates to see if one is greater than another:
If Format(Date, "m/d/yyyy") > #1/1/2000# Then MsgBox "Okay"
That's basically saying if today is later than January 1st, 2000 then pop up a message box.
This is fine if I am putting the date in the code (i.e., 1/1/2000), but I would like to get a date from the user during run-time and can't figure out how to incorporate that into the statement above.
If I provide a TextBox for the user to enter a date, I have surrounded the text with the # symbol:
If MyDate > "#" & Text1.Text & "#" then
That does not work. I've tried converting the text to Double, Integer, and Long. I have no idea how to use the enclosing # symbols with user input.
Thanks in advance for your help.

Use CDate(Text1.Text) to convert a string to a date. In reality you will want to do some checking to make sure that the value entered is a valid date, something along the lines of:
Dim myDate As Date
If IsDate(Text1.Text) Then
MyDate = CDate(Text1.Text)
Else
'Indicate the error to the user
End If
The # characters are "date quotes", if you will - they serve the same purpose for date literals as " does to delimit a string literal. You can't just concatenate # to a value to make it into a date.

Related

How to delete space in character text?

I wrote a code that automatically pulls time-related information from the system. As indicated in the table is fixed t247 Month names to 10 characters in length. But it is a bad image when showing on the report screen.
I print this way:
WRITE : 'Bugün', t_month_names-ltx, ' ayının'.
CONCATENATE gv_words-word '''nci günü' INTO date.
CONCATENATE date ',' INTO date.
CONCATENATE date gv_year INTO date SEPARATED BY space.
TRANSLATE date TO LOWER CASE.
I tried the CONDENSE t_month_names-ltx NO-GAPS. method to delete the spaces, but it was not enough.
After WRITE, I was able to write statically by setting the blank value:
WRITE : 'Bugün', t_month_names-ltx.
WRITE : 14 'ayının'.
CONCATENATE gv_words-word '''nci günü' INTO date.
CONCATENATE date ',' INTO date.
CONCATENATE date gv_year INTO date SEPARATED BY space.
TRANSLATE date TO LOWER CASE.
But this is not a correct use. How do I achieve this dynamically?
You could use a temporary field of type STRING:
DATA l_month TYPE STRING.
l_month = t_month_names-ltx.
WRITE : 'Bugün', l_month.
WRITE : 14 'ayının'.
CONCATENATE gv_words-word '''nci günü' INTO date.
CONCATENATE date ',' INTO date.
CONCATENATE date gv_year INTO date SEPARATED BY space.
TRANSLATE date TO LOWER CASE.
You can not delete trailing spaces from a TYPE C field, because it's of constant length. The unused length is always filled with spaces.
But after you assembled you string, you can use CONDENSE without NO-GAPS to remove any chains of more than one space within the string.
Add CONDENSE date. below the code you wrote and you should get the results you want.
Another option is to abandon CONCATENATE and use string templates (string literals within | symbols) for string assembly instead, which do not have the annoying habit of including trailing spaces of TYPE C fields:
DATA long_char TYPE C LENGTH 128.
long_char = 'long character field'.
WRITE |this is a { long_char } inserted without spaces|.
Output:
this is a long character field inserted without spaces

How to format input for SAS's MONYY format

I currently have a dataset with dates in the format "FY15 FEB". In attempting to format this variable for use with SAS's times and dates, I've done the following:
data temp;
set pre_temp;
yr = substr(fiscal,3,2);
month = substr(fiscal,6,length(fiscal));
mmmyy = month||yr;
input mmmyy MONYY5.;
datalines;
run;
So, I have the strings representing the year and corresponding month. However, running this code gives me the error "The informat $MONYY was not found or could not be loaded." Doing some background on this error tells me that it has something to do with passing the informat a value with the wrong type; what should I alter in order to get the correct output?
*Edit: I see on the SAS support page for formats that "MONYYw. expects a SAS date value as input;" given this, how do I go from strings to a different date format before this one?
When you see a $, it means character value. In this case, you're feeding SAS a character value and giving it a numeric format. SAS inserts the $ for you, but there is no such format in existence.
I'm going to ignore the datalines statement, because I'm not sure why it's there (though I do notice there is no set statement). You might have an easier time just changing your program to:
data temp;
yr = substr(fiscal,3,2);
month = substr(fiscal,6,length(fiscal));
pre_mmmyy = strip(month)||strip(yr);
mmmyy=input(pre_mmmyy,MONYY5.);
run;
you can also remove the "length(fiscal))" from the substring function. The 3rd argument to the substring function is optional, and will go to the end of the string by default.

Get tomorrow's date in MS Word insert field

I'm trying to insert date field in MS Word that will display tomorrow's date we one opens the document.
I can insert Today's date {DATE \# "dd/MM/yyyy"}, can we insert tomorrow's date using modified formula?
Thanks
Without VBA, the calculation is possible, but not straightforward, because the Word field language has very limited support for date-related operations.
Originally I thought Word would auto-update the DATE when you open or close/re-open the document, but further experiments suggest that even the second suggestion in here will not do that.
In the specific case described (add 1 day), you should be able to use the following field coding:
{QUOTE {SET xxx { DATE }}{SET yyy {xxx \#YYYY}}{SET mmm {xxx \#M}}{SET xxx1 {={xxx \#YYYYMMDD}+1 \#0000'-'00'-'00}}{=13-{xxx1 \#M} \#"'{xxx1}';'{=mmm-11 \#'{=yyy+1}-01-01';'{yyy}-{=mmm+1 \#00'-01'}'"}'"} \#DD/MM/YYYY}
All the {} must be the special field code brace pairs that you can insert on Windows Word using ctrl-F9, and (typically) on Mac Word using cmd-F9 or fn-cmd-F9, depending on your keyboard setup. You can change the format at the end ("\#DD/MM/YYYY" ) as required.
However, that set of field codes probably will not be updated automatically by Word when you open the document, so the user would need to select the field codes and press F9.
I originally thought Word would update the date on open and/or close/re-open using the following coding, but I now believe I was wrong. The one thing it does achieve on recent versions of Windows Word is to present the Date field in a "bubble" with an option to update the field:
{DATE \#"'{QUOTE {SET xxx { DATE }}{SET yyy {xxx \#YYYY}}{SET mmm {xxx \#M}}{SET xxx1 {={xxx \#YYYYMMDD}+1 \#0000'-'00'-'00}}{=13-{xxx1 \#M} \#"'{xxx1}';'{=mmm-11 \#'{=yyy+1}-01-01';'{yyy}-{=mmm+1 \#00'-01'}'"}'"} \#DD/MM/YYYY}'"}
Here is some pseudocode for the algorithm:
Set xxx to the date.
Set yyy to the 4-digit year
Set mmm to the month
Set xxx1 to the date but with the day number incremented by 1. e.g., for 2016-12-31, that would be a string, "2106-12-32"
This is the tricky bit:
Try to extract the month from that date using { xxx1 \#M }. If the date is valid, { xxx1 \#M } will return a valid month number, i.e. in the range 1 to 12. If the date is not valid, { xxx1 \#M } will return xxx1, e.g. "2106-12-32", which the { = } field will treat as a calculation, i.e. "year-(a maximum of 12+32=44)", so it is always going to return a number larger than 12.
If xxx1 is a valid date then
result=xxx1
Else 'xxx1 is not a valid date so...
If mmm (the original month) is 12 then
result = "(yyy+1)-01-01"
Else
result = "yyy-(mmm+1)-01"
End If
End If
Apply the date format you want to "result".
NB, this also relies on the assumption that Word always correctly recognises the month and day when you specify a date in the format "YYYY-MM-DD", regardless of the locale, in other words that "2016-04-01" is always recognised as 01 April 2016, never as 04 January 2016. If anyone can provide a counter-example, then the assumption is wrong, the field coding will need to change, and will probably need to be locale-dependent.

Find and replace when run as macro changes date value

I am having some trouble with find and replace. In my source workbook, I have text strings of project start dates. They read the first three letters of each day (Mon-Fri) then the associated date "Mon dd/mm/yyyy" for example. I need them in the form of dates "dd/mm/yyyy" so that I can use them after extraction in another workbook.
If I manually delete the "Mon " or use the replace all within the worksheet itself, I get the correct value of "dd/mm/yyyy" (converted to date format automatically). Serial number of the date 01/05/2015 for example being 42186.
However, when I use a macro to remove the "Mon " through the replace tool, the value of my dates changes to 05/01/2015. It is not a formatting error, but the actual value changing, new date serial number being 42009.
Any help as to why this is happening/how to stop it would be greatly appreciated!
I have included my macro replace code below.
Sub replace()
'Replace "Wed -Fri " with ""
Columns("C:C").Select
Selection.replace What:="Wed ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.replace What:="Fri ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.replace What:="Thu ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
Using the Range.Replace function in VBA will automatically convert the date to the default format of mm/dd/yyyy unless you override it. You should first try setting the formatting for Column C to Custom-> dd/mm/yyyy.
Adding this line to your VBA Sub right below Columns("C:C").Select will effectively do the same thing: Selection.NumberFormat = "dd/mm/yyyy"
For this issue though, I would probably just avoid VBA altogether and use something like =RIGHT(A1,LEN(A1)-4) assuming cell A1 contains Mon 01/05/2015
This would grab all characters in the cell except for the first 4. This of course requires data integrity where all days have only three letters and all have a space between the day and the date.
This is happening because, the system Regional format is in mm/dd/yyyy format, what you have to do is, after replacing the text, you should format the date to "dd/mm/yyyy" format

In Postgres, how would I convert the following text to a date?

In the following expression
"DATETIME"::date || ' 23:59:59.000'
I'm converting a DATETIME field to a pure date. The string concatenation changes the value to the very end of the day. I would now like to insert this value back into another DATETIME field but I am unsure how to convert from text back to DATETIME
This expression should work.
("DATETIME"::date || ' 23:59:59.000')::timestamp
Depending on your application, you might need to consider leap seconds. The 59th second isn't invariably the last second before the next date.
I assume the type of the DATETIME field is something like TIMESTAMP. If so, just cast the whole string back to a TIMESTAMP:
("DATETIME"::date || ' 23:59:59.00')::timestamp