Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

Need a function in C#/vb.net

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
0
Star79
Asked:
Star79
1 Solution
 
aaronchanCommented:
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
0
 
gangwischCommented:
Public Function nullToDate(ByVal val As Object) As DateTime
        Return Convert.ToDateTime(val).toshortdatestring
End Function
0
 
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.
0
 
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
else
    return now
end if
End Function
0
 
Mohamed ZedanCommented:
or you could
do this

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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