467,923 Members | 1,260 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

DateTime.Now - Does't give seconds.

DateTime tTime = DateTime.Now;
-------------------------------------------------------------------------------------------
tTime returns "1:59:00 PM" it never returns seconds.

Database field is SQL datetime or smalldatetime.

Apr 9 '07 #1
  • viewed: 12542
Share:
7 Replies
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"TheLostLeaf" <er**@canyondigital.comwrote in message
news:11*********************@o5g2000hsb.googlegrou ps.com...
DateTime tTime = DateTime.Now;
-------------------------------------------------------------------------------------------
tTime returns "1:59:00 PM" it never returns seconds.

Database field is SQL datetime or smalldatetime.

Apr 9 '07 #2
DateTime tTime = DateTime.Now;
Console.WriteLine(tTime.Hour.ToString());
Console.WriteLine(tTime.Second.ToString());
Console.WriteLine(tTime.Millisecond.ToString());

Cheers,
Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"TheLostLeaf" wrote:
DateTime tTime = DateTime.Now;
-------------------------------------------------------------------------------------------
tTime returns "1:59:00 PM" it never returns seconds.

Database field is SQL datetime or smalldatetime.

Apr 9 '07 #3
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
-

Apr 9 '07 #4
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"TheLostLeaf" <er**@canyondigital.comwrote in message
news:11*********************@l77g2000hsb.googlegro ups.com...
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
-

Apr 9 '07 #5
"TheLostLeaf" <er**@canyondigital.comwrote in message
news:11*********************@l77g2000hsb.googlegro ups.com...
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.


Apr 9 '07 #6
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 !!!
Apr 9 '07 #7
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

Apr 9 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by John Hunter | last post: by
3 posts views Thread by nriesch | last post: by
26 posts views Thread by Reny J Joseph Thuthikattu | last post: by
2 posts views Thread by John Reese | last post: by
6 posts views Thread by =?Utf-8?B?cm9kY2hhcg==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.