Solved

Need a function in C#/vb.net

Posted on 2006-10-19
5
200 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now