Converting String to Date type
I got an email with this request:
As useful as this function is, the principle it demonstrates shows how easy it is to use the Date Standard Library functions to manipulate date/time values from one format to another. For example, this next version just stretches the function to also return the time:
HTH,
Richard
So here's the code I wrote to do that:I need to convert incoming date like “Tue Jul 20 00:00:00 EDT 2010” into date type.
- Code: Select all
IMPORT Std;
ParseDate(STRING28 d) := FUNCTION
SetParts := Std.Str.SplitWords(d,' ');
str := SetParts[2] + ' ' + SetParts[3] + ' ' + SetParts[6];
fmt :='%b%t%d%t%Y';
RtnDate := STD.Date.FromStringToDate(str, fmt);
RETURN RtnDate;
END;
- Code: Select all
InDate := 'Tue Jul 20 00:00:00 EDT 2010';
ParseDate(InDate); //returns an integer value of 20100720
As useful as this function is, the principle it demonstrates shows how easy it is to use the Date Standard Library functions to manipulate date/time values from one format to another. For example, this next version just stretches the function to also return the time:
- Code: Select all
IMPORT Std;
ParseDateTime(STRING28 d) := FUNCTION
SetParts := Std.Str.SplitWords(d,' ');
DateStr := SetParts[2] + ' ' + SetParts[3] + ' ' + SetParts[6];
DateFmt :='%b%t%d%t%Y';
TimeFmt :='%H:%M:%S';
RETURN MODULE
EXPORT Date := STD.Date.FromStringToDate(DateStr, DateFmt);
EXPORT Time := STD.Date.FromStringToTime(SetParts[4], TimeFmt);
END;
END;
InDate := 'Tue Jul 20 12:32:45 EDT 2010';
ParseDateTime(InDate).Date; //returns 20100720
ParseDateTime(InDate).Time; //returns 123245
HTH,
Richard
- rtaylor
- Community Advisory Board Member
- Posts: 1409
- Joined: Wed Oct 26, 2011 7:40 pm
I ran across your post when trying to do a date conversion. I was able to use the function you demonstrate above to get my date conversion to work, but I am confused why my first attempt (code below) did not work? I tried it with a couple of different input formats with no success, so what I am misunderstanding about ConvertDateFormat?
- Code: Select all
date := '3/20/01 2:27 AM';
Std.Date.ConvertDateFormat(date, '%m/%d/%y %I:%M %p', '%Y%m%d');
- janet.anderson
- Posts: 29
- Joined: Wed Jul 17, 2013 5:31 pm
I am confused too in order to get it done!
subchorionichemorrhagehealth care tips
subchorionichemorrhagehealth care tips
Last edited by Rony Albert on Fri Apr 14, 2017 11:03 am, edited 1 time in total.
- Rony Albert
- Posts: 2
- Joined: Sat Apr 01, 2017 8:32 pm
Janet,
This code works for me:
HTH,
Richard
This code works for me:
- Code: Select all
date := '3/20/01 2:27 AM';
Std.Date.ConvertDateFormat(date, '%m/%d/%y', '%Y%m%d');
HTH,
Richard
- rtaylor
- Community Advisory Board Member
- Posts: 1409
- Joined: Wed Oct 26, 2011 7:40 pm
rtaylor wrote:Janet,
This code works for me:I think you were "confusing" the function with the time formatting characters when you're really only interested in the date.
- Code: Select all
date := '3/20/01 2:27 AM';
Std.Date.ConvertDateFormat(date, '%m/%d/%y', '%Y%m%d');
HTH,
Richard
Now it is understood thank you so much for helping me to get through it!
tachophobia
ipledges
- Rony Albert
- Posts: 2
- Joined: Sat Apr 01, 2017 8:32 pm
5 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 0 guests