Solved

Need a function in C#/vb.net

Posted on 2006-10-19
5
218 Views
Last Modified: 2010-04-23
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
Comment
Question by:Star79
5 Comments
 
LVL 5

Expert Comment

by:aaronchan
ID: 17771208
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
 
LVL 10

Expert Comment

by:gangwisch
ID: 17771229
Public Function nullToDate(ByVal val As Object) As DateTime
        Return Convert.ToDateTime(val).toshortdatestring
End Function
0
 

Author Comment

by:Star79
ID: 17772818
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
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17773925
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
 
LVL 7

Accepted Solution

by:
Mohamed Zedan earned 500 total points
ID: 17773946
or you could
do this

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question