DateTime.Now - Does't give seconds.

  • Thread starter Thread starter TheLostLeaf
  • Start date Start date
Actually, it does. 00 is the seconds. This is reflected in the Second
property on the DateTime structure.

Now, given that you are getting this from a database, it's possible that
you are not storing the seconds in the database in the first place. But the
DateTime structure in .NET most definitely does have a mechanism to get the
seconds at a point in time.

Hope this helps.
 
Sorry my post was so short, i didn't explain it properly.

I am trying to store the date time in the database, I was only
pointing our that the SQL field was not the issue with ommitting the
seconds.

I am just trying to get a DateTime string into the database (datetime
field) so that it includes the seconds.

For instance i am getting this
"1:59:00 PM"

and I want to get this

"1:59:29 PM"

Do I have to break it down into hours,minutes,seconds and re-
concantenate it ?


Thanks
-
 
No, you don't, but without seeing how you are inserting the value into
the database it's hard to say.

If you are using the datetime type in SQL Server, you should see second
values. The smalldatetime does not have second values, it will only show up
to minutes. From the documentation for Date and Time:

The smalldatetime data type stores dates and times of day with less
precision than datetime. The Database Engine stores smalldatetime values as
two 2-byte integers. The first 2 bytes store the number of days after
January 1, 1900. The other 2 bytes store the number of minutes since
midnight.
 
TheLostLeaf said:
Sorry my post was so short, i didn't explain it properly.

I am trying to store the date time in the database, I was only
pointing our that the SQL field was not the issue with ommitting the
seconds.

I am just trying to get a DateTime string into the database (datetime
field) so that it includes the seconds.

If you're losing seconds when the DateTime is stored in the database it's
because of the way you are storing or retriving the DateTime. I have no
problems storing DateTime fields in SQL datetime fields (and I get seconds
and fractions).

If you're using SQL smalldatetime then, that only stores minutes.
Do I have to break it down into hours,minutes,seconds and re-
concantenate it ?

No, you don't.

To retrieve I use:

DateTime myDateTime = dr.GetDateTime("myDateTime")

(Where dr is an open IDataReader)

To store I use:

updateCmd.Parameters.Add(new SqlParameter("@dtName", myDateTime))

Note that you have to make sure that myDateTime is within SQLs datetime
range which is smaller than a .NET DateTime.
 
Hmmm,

I think I figured it out. DateTime.Now does display the seconds, but
my data access layer must be ommiting them in a conversion somewhere.

It may be that I am using LLBLGen for my data layer.

Thanks for the feedback !!!
 
Ok I figured it out.

Since I was using LLBLGen to generate the C# Data access layer, my
original build had the field as smalldatetime, so the Layer retained
that conversion, even though I changed the SQL field to datetime. When
I rebuilt the Data Access Layer with the field as datetime, it workeds
perfect.

In the end i didn't realize smalldatetime eliminated seconds which was
the root of the problem, I though it only eliminated milliseconds..

Thanks for all the help ----

C# Noob pwnd by OO
 

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

Back
Top