I have a sql query which extract a field of datatype smalldatetime
. I read this field using datareader, convert it to string and store it in a string variable.
I get the string as 1/1/2012 12:00:00 AM ,here i want to use only date part of this string and i have nothing to do with the time.
How can i cut only the date from the string and get the output as 1/1/2012?
You may convert string to date type using Date.Parse() and use ToShortDateString()
Dim dt as Date = Date.Parse(dateString)
Dim dateString=dt.ToShortDateString()
As you said the type of field is smalldatetime then use DataReader.GetDateTime(column_ordinal).ToString("d")
method or DataReader.GetDateTime(column_ordinal).ToShortDateString()
You need to format the DateTime
using a format string for only dates.
string dateString = myDate.ToString("d");
This is equivalent to:
string dateString = myDate.ToShortDateString();
You can convert the string to Date and then get the Date part of the variable:
Dim myDateTime as Date = Date.Parse(dateString)
Dim MyDate as Date = myDateTime.Date()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With