How to retrieve datetime value in long time?

G

GoodMorningSky

Hi all,
The actual data is
10/12/2004 6:04:06 PM
being retrieved.

However,
when I retrieve it using ExectueScalar();
strSql = "SELECT LastTransferedTime FROM tblDepartment WHERE
DepartmentCode = 'HRD'"

cnn = new SqlConnection(DBHandler.HQstrCnn);
cmd = new SqlCommand(strSql, cnn);
cmd.Connection.Open();
result = cmd.ExecuteScalar();
cmd.Connection.Close();

It returns short date format
?result
{10/12/2004}
[System.DateTime]: {10/12/2004}

I need long datetime format which is '10/12/2004 6:04:06 PM' (real data in
DB)
How can I do that?
and what's problem here?
 
W

W.G. Ryan eMVP

A DateTime is going to have a time on it one way or the other so i think
it's an issue of how you're formatting it. If you use (just as a test)
result.ToLongTimeString(); what value do you get? or Result.Hour?
 
G

Guest

here is what you need to enter to format your string correctly. This is just
an example.

?result.ToString("MM/dd/yyyy hh:mm:ss tt")
"10/13/2004 09:18:29 AM"

this will work as long as result is a DateTime type, if it is not than an
exception will be thrown. If you want to know more info about hot to write
your own custome format strings then to here --
http://msdn.microsoft.com/library/d...ide/html/cpconcustomdatetimeformatstrings.asp

this should do it for you.

W.G. Ryan eMVP said:
A DateTime is going to have a time on it one way or the other so i think
it's an issue of how you're formatting it. If you use (just as a test)
result.ToLongTimeString(); what value do you get? or Result.Hour?

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
GoodMorningSky said:
Hi all,
The actual data is
10/12/2004 6:04:06 PM
being retrieved.

However,
when I retrieve it using ExectueScalar();
strSql = "SELECT LastTransferedTime FROM tblDepartment WHERE
DepartmentCode = 'HRD'"

cnn = new SqlConnection(DBHandler.HQstrCnn);
cmd = new SqlCommand(strSql, cnn);
cmd.Connection.Open();
result = cmd.ExecuteScalar();
cmd.Connection.Close();

It returns short date format
?result
{10/12/2004}
[System.DateTime]: {10/12/2004}

I need long datetime format which is '10/12/2004 6:04:06 PM' (real data in
DB)
How can I do that?
and what's problem here?
 
G

GoodMorningSky

The actual problem comes with data binding to DataGrid.
If I bind DataTable to datagrid.
The datetime value is appear as Short time format.
ToString() returns long date/time formate.
I want datagrid to show this format not the short time format.
How can I do this?
W.G. Ryan eMVP said:
A DateTime is going to have a time on it one way or the other so i think
it's an issue of how you're formatting it. If you use (just as a test)
result.ToLongTimeString(); what value do you get? or Result.Hour?

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
GoodMorningSky said:
Hi all,
The actual data is
10/12/2004 6:04:06 PM
being retrieved.

However,
when I retrieve it using ExectueScalar();
strSql = "SELECT LastTransferedTime FROM tblDepartment WHERE
DepartmentCode = 'HRD'"

cnn = new SqlConnection(DBHandler.HQstrCnn);
cmd = new SqlCommand(strSql, cnn);
cmd.Connection.Open();
result = cmd.ExecuteScalar();
cmd.Connection.Close();

It returns short date format
?result
{10/12/2004}
[System.DateTime]: {10/12/2004}

I need long datetime format which is '10/12/2004 6:04:06 PM' (real data in
DB)
How can I do that?
and what's problem here?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top