Thu Aug 16, 2018 7:04 am
Login Register Lost Password? Contact Us


Converting String to Date type

Share ideas, code, best practices and techniques with other community members

Mon Jan 23, 2017 8:27 pm Change Time Zone

I got an email with this request:
I need to convert incoming date like “Tue Jul 20 00:00:00 EDT 2010” into date type.
So here's the code I wrote to do that:
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;
This simply makes use of the Std.Str.SplitWords() function to parse out the discrete parts from the input date string which the STD.Date.FromStringToDate() function can then convert to the Date_t (AKA - UNSIGNED4) integer format.

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
Community Advisory Board Member
 
Posts: 1369
Joined: Wed Oct 26, 2011 7:40 pm

Mon Apr 10, 2017 5:15 pm Change Time Zone

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

Tue Apr 11, 2017 11:34 pm Change Time Zone

I am confused too in order to get it done!
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

Wed Apr 12, 2017 7:20 pm Change Time Zone

Janet,

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');
I think you were "confusing" the function with the time formatting characters when you're really only interested in the date.

HTH,

Richard
rtaylor
Community Advisory Board Member
Community Advisory Board Member
 
Posts: 1369
Joined: Wed Oct 26, 2011 7:40 pm

Mon May 01, 2017 7:05 am Change Time Zone

rtaylor wrote:Janet,

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');
I think you were "confusing" the function with the time formatting characters when you're really only interested in the date.

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


Return to Tips & Tricks

Who is online

Users browsing this forum: No registered users and 1 guest