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
Related
I'm trying to get DAYID as string in format YYYYMMDD, however its not working correctly.
I have a timestamp field, I take first 10 characters and convert it into date (working correctly)
toDate(substring(EventTimestamp,1,10))
-- 2021-03-24
However when I try to convert to string using below expression, I;m getting wrong answer. I;m getting the Day as 83.
toString(toDate(substring(EventTimestamp,1,10)),'YYYYMMDD')
--20210383
Date format characters are case-sensitive as per this answer here, so use yyyyMMdd instead.
An example using formatDateTime:
#formatDateTime(utcnow(), 'yyyyMMdd')
In my code below I am unable to convert the date format in column 1 from dd/MM/yyyy to MM-dd-yyyy. How do I configure it correctly to stop seeing this error??
>> T = readtable("IGEv3.xlsx");
>> tday=T{2:end, 1};
>> tday=datestr(datenum(tday, 'dd-MM-yyyy'), 'MM-dd-yyyy')
Error using datetime/datenum
Too many input arguments.
That looks like your Date column contains datetime values instead of strings or datenum doubles. (Strings or cellstrs show up with quotes in table display output. And the error message "Error using datetime/datenum" means that you're calling datenum() on a datetime input.)
datetime values support datestr calls directly. You can probably just do this:
tday=datestr(T{2:end,1}, 'mm-dd-yyyy')
And personally, I find it more readable when you reference variables by name instead of index:
tday = datestr(T.Date(2:end), 'mm-dd-yyyy')
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.
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.
I am having a problem converting a date/time string to an NSDate.
The date string I need to convert is: 2002-12-24T00:00:00-07:00
My date format string is currently yyyy-MM-dd'T'HH:mm:ssZZZ
The problem is the colon in the time zone.
ZZZ returns a string like: -0700 (without the colon)
How can I convert a string WITH a colon in the time zone?
Thanks
I suggest doing some string manipulation so it is in a form that dateWithString can more easily accept - how about using stringByReplacingOccurrencesOfString one or more times to get rid of the colon?
dateWithString wants:
YYYY-MM-DD HH:MM:SS ±HHMM
you have:
yyyy-MM-dd'T'HH:mm:ssZZZ
You will probably need to use some combination of componentsSeparatedByString (to get rid of the 'T' part, unless you have a small range of values possible for T, and perhaps write yourself a small function to convert ssZZZ into +HHMM.