Hot Topic (More than 10 Replies) Printstring & date format (Read 3439 times)
beagle
Junior Member
**
Offline


No personal text

Posts: 80
Location: australia
Joined: May 28th, 2004
Printstring & date format
Sep 17th, 2004 at 4:33am
Print Post Print Post  
I'm after some advice regarding printing a date using printstring

The date field in the layout is formatted to appear as DD/MM/YYYY.

However when printing, using printstring the date is printed as YYYY/MM/DD

Is there any way I can get the date to print as it appears on the form (DD/MM/YYYY)

Many thanks in advance
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Printstring & date format
Reply #1 - Sep 18th, 2004 at 12:15pm
Print Post Print Post  
All dates are stored as YYYY/MM/DD. You can reformat them any way you want by grabbing the pieces and stringing them together in whatever order you want.

Without leading zeroes - 19/6/2003
Code
Select All
var vFDate as String

vFDate = @Str(@DOM(MyDate)) + "/" + @Str(@Month(MyDate)) + "/" + @Str(@Year(MyDate)) 



With leading zeroes - 19/06/2003
Code
Select All
var vFDate as String

vFDate = @Text(2 - @Len(@Str(@DOM(MyDate))), "0") + @Str(@DOM(MyDate)) + "/" + @Text(2 - @Len(@Str(@Month(MyDate))), "0") + @Str(@Month(MyDate)) + "/" + @Str(@Year(MyDate)) 



Then feed the variable to PrintString:
PrintString(vFDate, ...)
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Printstring & date format
Reply #2 - Sep 18th, 2004 at 1:06pm
Print Post Print Post  
Quote:
With leading zeroes - 19/06/2003
Code
Select All
var vFDate as String

vFDate = @Text(2 - @Len(@Str(@DOM(MyDate))), "0") + @Str(@DOM(MyDate)) + "/" + @Text(2 - @Len(@Str(@Month(MyDate))), "0") + @Str(@Month(MyDate)) + "/" + @Str(@Year(MyDate)) 



Erika,

How about this shorter (and less complex) version of code to get the same results of your second example? Wink

Code
Select All
vFDate =  @rt(MyDate, 2) + "/" + @mid(MyDate, 6, 2) + "/" + @lt(MyDate, 4) 


  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Printstring & date format
Reply #3 - Sep 18th, 2004 at 1:25pm
Print Post Print Post  
Quote:
Erika,

How about this shorter (and less complex) version of code to get the same results of your second example? Wink

Code
Select All
vFDate =  @rt(MyDate, 2) + "/" + @mid(MyDate, 6, 2) + "/" + @lt(MyDate, 4) 




Carl,

Your version isn't type-safe. It depends on automagically converting the date to a string. This is often not the case. In SBasic, dates are dates, not strings pretending to be dates. SBasic will try to make your way work, and will often succeed, but its best not to depend on implied type conversions. If you've got a date, it's best to treat it like a date.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Printstring & date format
Reply #4 - Sep 18th, 2004 at 1:47pm
Print Post Print Post  
If it is stored internally as YYYY/MM/DD, why wouldn't it always work?

I tested it in a mass update of 1841 records, and it consistantly produced the same results as your code, with one exception: blank dates produced "00/00/0000" with my code, and "00/00/0" with your code. (I prefer my results on blank values.)

Can you give an example of a where my code would fail?
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Printstring & date format
Reply #5 - Sep 18th, 2004 at 2:07pm
Print Post Print Post  
The danger in treating a date (or any non-string type) as string is that you are depending on an internal coincidence of implementation that can change without warning. It is not part of the language specification that dates are presented as strings - thus it can change without warning in future releases.

We will make sure that @year() will always work. But there is no guarantee that grabbing the first four characters of a date (treated like a string, but not actually converted) will.

If you want to treat any field type as a different field type, always convert it to the needed type.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Printstring & date format
Reply #6 - Sep 18th, 2004 at 2:17pm
Print Post Print Post  
Quote:
...it can change without warning in future releases.


Thanks, Mark. That makes sense to me. I was having a hard time being convinced that there was a problem with it, since it seemed to be working perfectly now, in this version.
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Printstring & date format
Reply #7 - Sep 18th, 2004 at 2:23pm
Print Post Print Post  
Quote:
Thanks, Mark. That makes sense to me. I was having a hard time being convinced that there was a problem with it, since it seemed to be working perfectly now, in this version.


It only works well in this version because Mark and Andreas, knowing that Q&Aers often treat dates as strings, rather than using the date access functions, like @Year, went to some effort to try to turn the date back into a string under the hood when someone tries to use String functions on it. They may not always be able to protect you from these kinds of issues in the future.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Printstring & date format
Reply #8 - Sep 18th, 2004 at 2:54pm
Print Post Print Post  
Erika,

Thanks for that insight. I did not realize that they had made special concessions for my old habits. Smiley  I definitely have a lot of mindsets based on my experience with Q&A.
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
beagle
Junior Member
**
Offline


No personal text

Posts: 80
Location: australia
Joined: May 28th, 2004
Re: Printstring & date format
Reply #9 - Sep 18th, 2004 at 11:13pm
Print Post Print Post  
Thanks a lot guys - all is becoming clear to me ......slowly

  
Back to top
 
IP Logged
 
Steve_in_Texas
Senior Member
*****
Offline


No personal text

Posts: 893
Location: San Antonio
Joined: Feb 21st, 2004
Re: Printstring & date format
Reply #10 - Nov 14th, 2004 at 9:54pm
Print Post Print Post  
Thanks for this thread. It helped me out with printing some invoices.

Is there a similar command to convert 24 hour time format to 12 hour format?

Thanks,
Steve
  
Back to top
IP Logged