473,513 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DateTime Problem

string var = DateTime.Today.Month.ToString() + "-" +
DateTime.Today.Day.ToString() + "-" + DateTime.Today.Year.ToString() + "-" +
DateTime.Today.Hour.ToString() + "-" + DateTime.Today.Minute.ToString() +
"-" + DateTime.Today.Second.ToString();
MessageBox.Show(var);

returns 12-21-2006-0-0-0

Why would the hours, minute and second not appear correctly?

Dec 22 '06 #1
6 10747
Using DateTime.Now works. I overlooked Now.
"Jake K" <no*****@address.comkwrote in message
news:eu*************@TK2MSFTNGP02.phx.gbl...
string var = DateTime.Today.Month.ToString() + "-" +
DateTime.Today.Day.ToString() + "-" + DateTime.Today.Year.ToString() + "-"
+ DateTime.Today.Hour.ToString() + "-" + DateTime.Today.Minute.ToString()
+ "-" + DateTime.Today.Second.ToString();
MessageBox.Show(var);

returns 12-21-2006-0-0-0

Why would the hours, minute and second not appear correctly?
Dec 22 '06 #2
The hours, minute and second appear absolutely correctly because
DateTime.Today returns a DateTime object with just the date, i.e. the time
part all set to 0 (midnight).

Have a look at The DateTime.ToString(format) overloads to make life easier
for yourself.

MessageBox.Show(DateTime.Today.ToString("M-d-y-H-m-s")) and
MessageBox.Show(DateTime.Now.ToString("M-d-y-H-m-s"))

will display:

12-21-2006-0-0-0 and
12-21-2006-12-34-56

respectively if the time is 12:34:56 PM on 21/12/2006.
"Jake K" <no*****@address.comkwrote in message
news:eu*************@TK2MSFTNGP02.phx.gbl...
string var = DateTime.Today.Month.ToString() + "-" +
DateTime.Today.Day.ToString() + "-" + DateTime.Today.Year.ToString() + "-"
+ DateTime.Today.Hour.ToString() + "-" + DateTime.Today.Minute.ToString()
+ "-" + DateTime.Today.Second.ToString();
MessageBox.Show(var);

returns 12-21-2006-0-0-0

Why would the hours, minute and second not appear correctly?

Dec 22 '06 #3
I realise that the problem is resolved, but can I offer some advice?
Either use the format specifiers, or cache the date first, otherwise
you could get anomolies if (for instance) the date ticks over midnight
between calls... and on newyear you could end up printing dates a year
in the future... as in:
[end of 2006]
var = (Dec) + (12)...
[<tick, fireworks, etcnow January]
...+(2007)+(0)+(0)+(0)

i.e.
DateTime when = DateTime.Now;
and work with "when"; the overloads on .ToString() are good too, but
already mentioned by another poster.

Marc

Dec 22 '06 #4
Thanks for the replies. I appreciate the feedback.
"Marc Gravell" <ma**********@gmail.comwrote in message
news:11*********************@f1g2000cwa.googlegrou ps.com...
>I realise that the problem is resolved, but can I offer some advice?
Either use the format specifiers, or cache the date first, otherwise
you could get anomolies if (for instance) the date ticks over midnight
between calls... and on newyear you could end up printing dates a year
in the future... as in:
[end of 2006]
var = (Dec) + (12)...
[<tick, fireworks, etcnow January]
...+(2007)+(0)+(0)+(0)

i.e.
DateTime when = DateTime.Now;
and work with "when"; the overloads on .ToString() are good too, but
already mentioned by another poster.

Marc
Dec 22 '06 #5

Stephany Young wrote:
Have a look at The DateTime.ToString(format) overloads to make life easier
for yourself.
Here's the reference page, to make life even easier...
http://msdn2.microsoft.com/en-us/lib...ormatinfo.aspx

Dec 22 '06 #6
I find it much easier to press the F1 key.
"Bobbo" <ro************@choicequote.co.ukwrote in message
news:11**********************@48g2000cwx.googlegro ups.com...
>
Stephany Young wrote:
>Have a look at The DateTime.ToString(format) overloads to make life
easier
for yourself.

Here's the reference page, to make life even easier...
http://msdn2.microsoft.com/en-us/lib...ormatinfo.aspx

Dec 22 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2805
by: Mingus Tsai | last post by:
Hello- please help with unpickling problem: I am using Python version 2.3.4 with IDLE version 1.0.3 on a Windows XPhome system. My problem is with using cPickle to deserialize my pickled...
2
15861
by: Rey | last post by:
Howdy all. My problem deals w/inserting nulls into database (SQL Svr 2K) for the datetime fields activityDate and followUpDate where nulls are allowed. >From the web form, the user can type...
8
1853
by: Alan Silver | last post by:
Hello, I have a custom control that displays the date and time. I currently have properties for the day, month, year, hour and minute, which have to be set separately. This is inefficient. I...
5
1981
by: I am Sam | last post by:
I have created this DateTime object and instanced it I think correctly DateTime myClubNow1=new...
9
4899
by: Phil B | last post by:
I am having a problem with a datetime from a web services provider The provider is sending the following SOAP response <?xml version="1.0" encoding="utf-8"?> <soap:Envelope...
3
3118
by: pranesh.nayak | last post by:
I'm facing an error:"String was not recognized as a valid DateTime" whille accessing DateTime from webservice. And when I try to set DateTime to the same webservice it fails with error:"Date...
6
1844
by: JFieseler | last post by:
Hi all, in a huge project i have the following problem. I create an object which contains many private members (i know that this is not correct, but it is a single use migration program). The...
5
2582
by: iulian.ilea | last post by:
Is correct to have a varchar field and insert dates of type dd/mm/yyyy into it? I choose this method because I have an application that runs on more than one server. So, if I used a datetime field...
4
7213
by: =?Utf-8?B?QmFidU1hbg==?= | last post by:
Hi, I have a GridView and a SqlDataSource controls on a page. The SqlDataSource object uses stored procedures to do the CRUD operations. The DataSource has three columns one of which -...
4
3683
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I am having a problem formatting a string when the time is in format hh.mm.ss - used in Europe Parse seems ok when the date uses "/" or "." as seperator but I get an exception when time...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.