Need a function in C#/vb.net

Star79
Star79 used Ask the Experts™
on
Hi,

This is the Function i am using for validating dates.

Public Function nullToDate(ByVal val As Object) As DateTime
        Return Convert.ToDateTime(val)
End Function

Input string as follows:   2005-09-01T00:00:00.0000000-04:00

Output:
a)Time zone : Central time   #8/31/2005 11:00:00 PM#  
b)Time zone : Eastern  9/1/2005  
c)Time zone : Pacific time  8/31/2005 9:00:00 PM

The above function gives three different results in three different time zone for the given input string.
I want to write a function which gives just only the date for the above input and in different time zones.

your reply will be highly appreciated
Thanks,
Rithesh
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can force your return as String with mm/dd/yyyy format:

Public Function nullToDate(ByVal val As Object) As String
        Return Convert.ToDateTime(val).ToString("MM/dd/yyyy")
End Function
Public Function nullToDate(ByVal val As Object) As DateTime
        Return Convert.ToDateTime(val).toshortdatestring
End Function

Author

Commented:
The above two does not work. If you read my question carefully , it is changing the date in other zones.

if the input string is 2005-09-01T00:00:00.0000000-04:00

then the output should be  2005-09-01 not what in what time zone you are.
Mohamed ZedanSenior IT Analyst

Commented:
if the input is in that format only then use this

Public function NullToDate(val as object) as datetime
dim ReturnDate as date = now
if isdate(val) then
    returndate = new date(val.substring(0,4),val.substring(5,2),val.substring(8,2))
    return returndate
else
    return now
end if
End Function
Senior IT Analyst
Commented:
or you could
do this

Public Function nullToDate(ByVal val As Object) As DateTime
        Return Convert.ToDateTime(val).ToUniversalTime
End Function

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial