Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Need a function in C#/


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

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
1 Solution
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
Star79Author 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 ZedanCommented:
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
    return now
end if
End Function
Mohamed ZedanCommented:
or you could
do this

Public Function nullToDate(ByVal val As Object) As DateTime
        Return Convert.ToDateTime(val).ToUniversalTime
End Function
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now