Friday, September 26, 2008

String to DateTime conversion and Vice Versa

'String to DateTime

VB.Net
Dim dateString As String
dateString = "1999-09-01 21:34 PM"
Dim datetimeStamp As New DateTime
datetimeStamp = DateTime.ParseExact(dateString, "yyyy-MM-dd HH:mm tt", Nothing)

C#
//
String to DateTime
String
MyString;
MyString = "1999-09-01 21:34 PM";
DateTime MyDateTime;
MyDateTime = new DateTime();
MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt", null);

VB.Net
'DateTime to String
datetimeStamp = New DateTime(1999, 9, 1, 21, 34, 0)
dateString = datetimeStamp.ToString("yyyy-MM-dd HH:mm tt")

C#
//
DateTime to String
MyDateTime = new DateTime(1999, 09, 01, 21, 34, 00);
String
MyString;
MyString = MyDateTime.ToString("yyyy-MM-dd HH:mm tt");

No comments: