bpayne111 Posted September 17, 2003 Posted September 17, 2003 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 Quote i'm not lazy i'm just resting before i get tired.
Hamburger1984 Posted September 17, 2003 Posted September 17, 2003 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... Quote
bpayne111 Posted September 17, 2003 Author Posted September 17, 2003 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? Quote i'm not lazy i'm just resting before i get tired.
*Experts* mutant Posted September 17, 2003 *Experts* Posted September 17, 2003 You can use the TimeSpan object to see the difference between two dates: 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 Quote
Leaders John Posted September 18, 2003 Leaders Posted September 18, 2003 (edited) Re: Retrieving current date and time from system. ...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] Edited September 18, 2003 by John Quote "These Patriot playoff wins are like Ray Charles songs, Nantucket sunsets, and hot fudge sundaes. Each one is better than the last." - Dan Shaughnessy
bpayne111 Posted September 18, 2003 Author Posted September 18, 2003 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 Quote i'm not lazy i'm just resting before i get tired.
*Experts* mutant Posted September 18, 2003 *Experts* Posted September 18, 2003 If you just want to get the days only then its very similar: 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 Quote
GraysonPeddie Posted September 23, 2003 Posted September 23, 2003 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: // 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! Quote
wyrd Posted September 23, 2003 Posted September 23, 2003 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 Quote Gamer extraordinaire. Programmer wannabe.
*Gurus* divil Posted September 24, 2003 *Gurus* Posted September 24, 2003 My personal favourite is to take advantage of operator overloading so you can use normal operators to work with dates and timespans: DateTime oldDate = DateTime.Parse("11/9/03 12:00"); TimeSpan timePassed = DateTime.Now - oldDate; Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
bpayne111 Posted September 25, 2003 Author Posted September 25, 2003 yup once i found the objects i was looking for everything came together just wonderfully. thanks for the help guys Quote i'm not lazy i'm just resting before i get tired.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.