Retrieving current date and time from system. (C#)

bpayne111

Junior Contributor
Joined
Feb 28, 2003
Messages
325
Location
BFE
Retrieving current date and time from system.

I understand that there are math controls for dates and times but how do i get the current date and time so that i may do some math with it?

and is this thread considered syntax specific? so that next time i will post things like this correclty?

thanks
brandon
 
I don't really understand what you're asking for.. if you want the current Date and Time on a Machine just do something like:

DateTime myDateTime = DateTime.Now;

if this wasn't what you were looking for, please ask again...
 
yes that is what i'm looking for
but i'm going to need to display this compare this date and time with another to see how many days are in between... is that going to be complicated?
 
You can use the TimeSpan object to see the difference between two dates:
Visual Basic:
Dim b As Date = Date.Now
Dim b1 As Date = Date.Now.AddDays(2) 'add two days to the current date
'so there is some kind of a difference to compare :)
Dim ts As TimeSpan = b1.Subtract(b) 'subtract the first date
'and return it to the timespan object
MessageBox.Show(ts.ToString) 'show the difference
 
Re: Retrieving current date and time from system.

bpayne111 said:
...and is this thread considered syntax specific? so that next time i will post things like this correclty?...
No, it is more related to the framework than any one language. You can denote the language if you want in the title of the thread. I have edited this thread to reflect the fact that you are using VB since that is where this thread was before I moved it.

Syntax specific is for things which apply only to a given language, not things which have to do with the framework and other things listed in the descriptions of each forum on the home page.

C++ is a different story all together though IMHO.

[edit]*sigh*, my mistake. This thread was indeed moved from the C# board. :-\[/edit]
 
Last edited:
actually i'm using C#

vb is fine though i can just switch syntaxes a lil...
this timespan function... what kind of value does it return?
i'd like number of days but i can do math if i have to.
thansk for the help guys
 
If you just want to get the days only then its very similar:
C#:
DateTime d = DateTime.Now;
DateTime d1 = DateTime.Now.AddDays(3);
TimeSpan ts = d1.Subtract(d);
MessageBox.Show(ts.TotalDays.ToString()); //use the TotalDays property to get days only
 
Tip!

I got a tip for you all. If you want to display a current time realtime, create a timer object and assign an interval to be 1000 and set the Enabled property to true. Declare a DateTime variable and put in the code inside the timer event. Assign a DateTime variable to the label control once you convert to string by calling like:

labelName.Text = DT.ToString();

Where labelName is assigned to a label control and DT is a DateTime Variable.

To give you a better explaination, create a Visual C# project called WFormClock. (W stands for Windows.) Note that I have Visual C# .net.

In Solutions Explorer, right click WFormClock, go to Add, and click Add User Control. There, type Clock and click Okay. This creates a user control designer, where you can have 2+ controls inside one user control (for example, a label and a timer object).

In the Windows Form Toolbox, grab the two items: the label, and the Timer.

Rename 2 controls:
label1 = lblClock
timer1 = tClock

Dock the lblClock's control to the center and set the TextAlign property to Middle.

In the tClock's property, set the Interval to 1000 and enabled property to true. Double-click the tClock component and declare the DateTime Control at the top of the tClock event.

In the tClock event, write the code:

Code:
// C#
private void tClock_Tick(object sender, System.EventArgs e)
{
    DateTime DT = DateTime.Now;
    lblClock.Text = DT.ToString();
}

Compile the control and bring the Clock control to the Windows Form application. Clock can be found under the Windows Form Toolbox. When you do add the clock control to the project, notice the control right away!

What it does is the control shows you the clock real-time!

Compile Widnows Form (WFormClock) and Start Without Debug.

Pretty simple! It's very easy to do, and very easy to code!
 
There are tons of methods that the DateTime class offers for manipulating dates.

AddDays, AddMonths, AddYears, AddSeconds, AddMinutes, AddHours, etc. However to subtract you'll need to use the add methods with a negative number (-4). There's also Compare, Subtract, Equals, DaysInMonth, etc. And you can even convert the date to an int value (if I'm not mistaken).

Here's the documentation on it;
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDateTimeMembersTopic.asp?frame=true
 
My personal favourite is to take advantage of operator overloading so you can use normal operators to work with dates and timespans:

C#:
DateTime oldDate = DateTime.Parse("11/9/03 12:00");
TimeSpan timePassed = DateTime.Now - oldDate;
 
yup

once i found the objects i was looking for everything came together just wonderfully. thanks for the help guys
 
Back
Top