I would like to read the "19-APR-21 18.17.06.301100" the string into a DATETIME and display accordingly. In order to achieve that I'm intending to define a display format and an informat as provided in the example below.
Example
* Define formats to read and display data;
* Input format;
PROC FORMAT LIB=work;
PICTURE indte other = '%d-%B-%y %H.%M.%S.%s'
(DATATYPE=DATETIME);
RUN;
* Nice display format;
PROC FORMAT LIB=work;
PICTURE outdte other = '%d %B %y %H:%M'
(DATATYPE=DATETIME);
RUN;
* Test with sample data;
DATA text;
strdate = "19-APR-21 18.17.06.301100";
RUN;
* Apply informat and display format;
DATA pretty(RENAME=(strdate=nicedte));
SET work.text;
ATTRIB strdate INFORMAT=indte.;
FORMAT strdate outdte.;
RUN;
Problems/questions
The DATA pretty procedure is not able to load the define formats and returns the following error:
NOTE 485-185: Informat $INDTE was not found or could not be loaded.
NOTE 484-185: Format $OUTDTE was not found or could not be loaded.
Is this an efficient way of doing this for the data that in the proper process is sourced via: infile <file ref created by filename> delimiter=',' stopover firstobs=2 dsd lrecl=32767;?
PICTURE is used to create a FORMAT, not an INFORMAT.
The ANYDTDTM informat should handle strings in that style. But it will ignore the fractions of a second.
DATA text;
strdate = "19-APR-21 18.17.06.301100";
datetime = input(strdate,anydtdtm.);
format datetime datetime26.6 ;
put (_all_) (=/);
RUN;
Results:
strdate=19-APR-21 18.17.06.301100
datetime=19APR2021:18:17:06.000000
If you need the fractions of a second then parse the string and read it separately.
datetime = dhms(input(strdate,date9.)
,input(scan(strdate,2,' .'),2.)
,input(scan(strdate,3,' .'),2.)
,input(scan(strdate,4,' .'),2.)
+input(scan(strdate,5,' .'),6.6)
);
Related
Default DATE format when displaying dates is DD/MM/YY
I want to change that to DD.MM.YYYY
This is just a simple program:
DEFINE VARIABLE daDate AS DATE NO-UNDO.
daDate = TODAY.
MESSAGE daDate.
Currently the output looks like this: 16/09/20
I tried adding FORMAT "99.99.9999" after the variable name like this: DEFINE VARIABLE daDate FORMAT "99.99.9999" AS DATE NO-UNDO. but it didn't change the output at all.
When I instead of MESSAGE use DISPLAY and then write it out with FORMAT, then it displays the correct format: DISPLAY daDate FORMAT "99.99.9999".
Am I doing something completely wrong or am I missing something?
The expression you message will be converted to character first so instead you can take control over that conversion:
MESSAGE STRING(daDate,"99.99.9999").
DEFINE VARIABLE hoy AS CHARACTER NO-UNDO.
hoy = STRING (DAY (TODAY), "99") + "."
+ STRING (MONTH (TODAY), "99") + "."
+ STRING (YEAR (TODAY)) .
Message hoy.
I have a saved time that is in the format of this: moment().format('llll').
I want to convert that time to this format: YYYYMMDD?
I tried this:
let Time = moment().format('llll')
moment(Time, 'YYYYMMMDD')
That results in moment.invalid(/* 2019年7月4日星期四 16:03 */)
Anyone an idea on how to do it?
moment().format('llll') gives you a formatted string.
To create a moment object with a formatted string, you should also provide the string format.
let Time = moment().format('llll')
moment(Time, 'llll')
After that, you can easily format moment object as string
moment(Time, 'llll').format('YYYYMMMDD')
So the file I am extracting has multiple instances of two different starting time formats. One start time is in zulu (UTC) format and another is in a standard dateTime format. So when I create a SELECT they both have to pass through it.
An example of my UTC startingTime is 2011-01-02T00:03:04.123Z
An example of a standard startingTime is 2011-Jan-20 01:15:37.000941 EST
I need some sort of dateTime.Parse that can handle them both and return it to me in the same style the second one 2011-Jan-20 01:15:37.000941 EST is in.
Currently I am using DateTime.Parse(StartingTime).ToString("yyyy-MMM-dd HH:mm:ss.fff) As StartingTime"
This works for Parsing the UTC time format but gives me an error when trying to Parse the other.
Any ideas?
Even if I don't get it to return to me exactly like this one 2011-Jan-20 01:15:37.000941 EST I'll take something that shows three figures of milli seconds ex: 2011-Jan-20 01:15:37.941 EST which is what my code does now.
If you only have this kind of date you can try this.
You can add more types on the IF statement!
DECLARE #func Func<string,string> = (s) => {
DateTime d;
if (DateTime.TryParse(s, out d))
{
TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
d = DateTime.SpecifyKind(d, DateTimeKind.Unspecified);
DateTime cstTime = TimeZoneInfo.ConvertTime(d, cstZone, TimeZoneInfo.Utc);
return cstTime.ToString("yyyy-MMM-dd HH:mm:ss.ffffff EST");
}
return s;
};
#data =
SELECT data FROM
( VALUES
("2011-01-02T00:03:04.123Z")
,("2011-Jan-20 01:15:37.000941 EST")
) AS T(data);
#result =
SELECT #func(data) AS data FROM #data;
OUTPUT #result
TO "/test.txt"
USING Outputters.Tsv();
I have a nullable variable Start time
Timespan? st=e.StartTime;//Null-able variable;
I am trying to get time in AM/PM format but I am unable to get it.
DateTime date = Convert.ToDateTime(st.ToString());
String f = String.Format("{0:hh:mm:tt}", date);
Error is:
System.FormatException: String was not recognized as a valid DateTime.
If you were to output the results of st.ToString(), you will find that it doesn't contain any date information, only hours, minutes and seconds.
This isn't a valid format for a DateTime, which generally contain date and time information.
You don't need to convert your TimeSpan to a DateTime to format it, you can just use TimeSpan.ToString():
string f = st.Value.ToString(#"hh\:mm\:tt");
For reference: http://msdn.microsoft.com/en-us/library/ee372287.aspx
Also, note the \ before the :, you must do this if you want to include literal strings in the output, as mentioned at the bottom of that documentation page.
Converting a timespan to a date is not possible, a timespan represents x amount of minutes/hours/whatever and you cannot get an exact date from that alone. If you have a date as a starting point, you can add a timespan and that will give you the new date.
st.ToString() will return "System.Nullable<Timespan>" because that is what a nullable type returns - it does not override the default Object.ToString implementation, so returns the type name.
If you want the string of the actual timespan, then you would need to do st.Value.ToString(), but you should be checking for null first (i.e. st.HasValue == true)
Edit: Also see #Sean's comment about how to output the Timespan without converting to a DateTime first.
Edit: Turns out I was slightly wrong - st.ToString() doesn't return the above. So see Sean's answer.
First convert Timespan to Datetime by adding TimeSpan to a base date of 00:00 hrs. Then on that dateTime derive the 12 hr format.
DateTime.Now.Date.Add(OpenTimeSpan).ToString(#"hh\:mm\:tt")
The Accepted Answer is wrong.
You cannot return AM/PM for a TimeSpan because it is only concerned with the length of Time,
not a Time of Day - hench the name, "TimeSpan".
Convert to a DateTime first before converting to a String:
string sTimeOfDay = new DateTime().Add(st).ToString("hh:mm tt");
Note: If your TimeSpan is nullable, then you will need to add Conditional Logic to Handle Nulls and pass in ts.Value instead of ts:
string sTimeOfDay = (st == null ? null : new DateTime().Add(st.value).ToString("hh:mm tt") );
I'm parsing an XML file from an external source, and I have 2 attributes which contain the date and time respectively. I'm looking for the best way to get these into a format I can parse as a date so I can do things with it, but at the moment I'm just getting errors or no results with the methods I've tried.
The date is in the format "20111215" - which is yyyymmdd as it's UK based.
The time is formatted as "1417+0000" which I presume is the time plus offset from GMT?
Basically I need to get these into UK time. I've tried using DateTime.Parse on the separate parts but both give an error as not valid format. Tried String.Format on the date part but that didn't change it at all. I presume I need to combine the 2 before parsing but I'm not sure if I need to do anything else with it to make it acceptable.
Any help appreciated.
Use a DateTimeOffset to incorporate the timezone into the DateTime.
string date = "20111215";
string time = "1417+0500";
string dateAndTime = date + time;
string format = "yyyyMMddHHmmzzz";
CultureInfo provider = CultureInfo.InvariantCulture;
DateTimeOffset t = DateTimeOffset.ParseExact(dateAndTime, format, provider);
If you concatenate the fields together, you can then use DateTime.TryParseExact in order to parse them into a DateTime.
string input = string.Format("{0} {1}", dateString, timeString);
DateTime parsed;
if(DateTime.TryParseExact(input,
"yyyyMMdd HHmmK",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out parsed))
{
// parsed OK, use the parsed variable
}
string date = "20111215";
string time = "1417+0000";
string dateString = date + time;;
string format = "yyyyMMddHHmmK";
// or something similar, I'm not sure about the timezone
DateTime result = DateTime.ParseExact(dateString,
format,
CultureInfo.InvariantCulture);
I think this should work (i didn't test it):
string dateString = "20111215";
string timeString = "1417+0000";
int year = int.Parse(dateString.Substring(0,4));
int month = int.Parse(dateString.Substring(4,2));
int day = int.Parse(dateString.Substring(6,2));
int hour = int.Parse(dateString.Substring(0,2));
int mins = int.Parse(dateString.Substring(2,2));
DateTime d = new DateTime(year, month, day, hour, mins, 0);