Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do i format a string to date(mm/dd/yyyy) 1

Status
Not open for further replies.

umeshs79

Programmer
Aug 7, 2002
42
DE
Hi
i have date "2002-10-31T00:00:00" in string type variable.
This value is actually i am getting from XML and i am passing to report as parameter.
and now i format it to "mm/dd/yyyy" but when i try to convert it into date type it is giving error in type ocnversion.
So can anybody tell me how do i convert it to "mm/dd/yyyy" format.

Thanks.
Umesh
 
If you want your result to be a true date field, you can use:

stringvar dt:={your.date.field}
date(val(dt [1 to 4]),val(dt[6 to 7]),val(dt[9 to 10]))

Mike

 
No actually i want to format it in formula and after that i want to concatinate it with some string than want to return.
So i cannot directly format it by right clicking on the date field, format field, customize, date tab.
I need to do it in code.

So please tell me how i do it.

Thanks
Umesh
 
Try:

{MyTable.MyDate} [1 to 4]) + "/" +{MyTable.MyDate} [6 to 7]) + "/" + {MyTable.MyDate} [8 to 9])

You can concat your string to it by just adding another + {MyTable.MyConcatField} or something within quotes.

-k

kai@informeddatadecisions.com
 
Why do you have to do it in code? Sounds like a homework assignment.

Actually, date("08/07/2002") will return a true date, which is probably what you want provided that your database field is indeed a string.

If your formula returns a string rather than a true date, and you try to sort on it, you will get results like the following:

"01/15/2002"
"10/15/2002"
"11/15/2002"
"02/15/2002"
"04/15/2002"

If you make your result a date you will not have this sorting problem. Software Training and Support for Macola, Crystal Reports and Goldmine
714-348-0964
dgilsdorf@mchsi.com
 
Hi,
My actual problem was that the "2002-10-31T00:00:00" is not converted into date by using CDate() or Date() function it was giving error in conversion.
So to do that i have written following code.
ToText(date(Left({?dateToPrm},instr(1,{?dateToPrm},"T")-1)), "MM/dd/yyyy")

the above code trim the time part from string and than covert it into date.
It is working fine.

So anybody has better soluton of this please reply.

Thanks
Umesh


 
Your date is stored as a datetime, not a date, so your solution sorta makes sense, unless you want the time, in which case you'd use the datetime() function, making sure that the string format only excludes the "T".

BTW, the formula you've worked out doesn't create a date, it creates text (drop the totext).

Also, it appears that you're converting a parameter, why not just make the parameter a type of date or time?

-k kai@informeddatadecisions.com
 
Your date is stored as a datetime, not a date, so your solution sorta makes sense, unless you want the time, in which case you'd use the datetime() function, making sure that the string format only excludes the "T".

BTW, the formula you've worked out doesn't create a date, it creates text (drop the totext).

Also, it appears that you're converting a parameter, why not just make the parameter a type of date or datetime?

-k kai@informeddatadecisions.com
 
SV- it is a parameter that is passed in from XML so the user has no control over it.

You want a simplier solution then don't ignore Mbarron's suggestion...it was the best one IMHO...just modify it a little.

@ExtractDate

WhilePrintingRecords;
stringvar dt:= {?dateToPrm};
stringVar DateResult;

DateResult := totext(date(val(dt [1 to 4]),val(dt[6 to 7]),
val(dt[9 to 10])),"MM/dd/yyyy")

DateResult;

Now you have a date as a variable that can be used anywhere

Jim Broadbent
 
Sorry SV...read your original post and that works too :) Jim Broadbent
 
Thanks all of you.
i think the solution provided by Jim is better than mine.
So i have changed it.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top