Results 1 to 5 of 5

Thread: [2.0] date/time

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    [2.0] date/time

    how to set the time, using this code doesnt work;

    label1.caption = datetime.now
    label2.caption = time

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] date/time

    DateTime.Now returns a DateTime object containing the current date and time. DateTime.Today returns a DateTime object containing the current date with the time zeroed to midnight. If you want to display some or all of those values you need to convert them to strings, as you do with all object before displaying them in Labels. Note also that Labels have no Caption property:
    C# Code:
    1. DateTime currentTime = DateTime.Now;
    2.  
    3. label1.Text = currentTime.ToShortDateString();
    4. label2.Text = currentTime.ToShortTimeString();
    There are many variations that give you complete control over how the date and/or time is formatted too. See the DateTime.ToString method documentation for more information.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Registered User RaviIntegra's Avatar
    Join Date
    Mar 2007
    Location
    Pondicherry, India
    Posts
    125

    Re: [2.0] date/time

    Code:
    DateTime d=new DateTime();
    MessageBox.Show(d.Date.ToShortDateString());
    Try this code this will work

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] date/time

    Quote Originally Posted by RaviIntegra
    Code:
    DateTime d=new DateTime();
    MessageBox.Show(d.Date.ToShortDateString());
    Try this code this will work
    Gee, why didn't I think of that? Hang on, I did! By all means contribute but suggesting the same thing as someone else days afterwards isn't really a contribution.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Fanatic Member kregg's Avatar
    Join Date
    Feb 2006
    Location
    UK
    Posts
    524

    Re: [2.0] date/time

    Lol. Very true jmcilhinney.

    To OP: You can press F1 on the keyboard in Visual C# Express Edition and use the search feature. You'd be suprised by the amount of info they will have.

    EDIT: Changed first sentence after reading jmcilhinney's well explained post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width