How can I get current time in C#.NET?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: How can I get current time in C#.NET?

  1. #1
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    Angry How can I get current time in C#.NET?

    I develop an ASP.NET web application using C#.NET. How can I get current time?
    My code is
    DateTime time = DateTime.Now;

    But the result is that time equals to DateTime.MinValue. Why?

    If I edit the code like this:
    int time = DateTime.Now.Hour;
    I can get the real hour. Also I can get the string of current time by "Label1.Text = DateTime.Now.ToString();".

    Thanks in advance.
    Last edited by jasonli; December 4th, 2005 at 02:37 AM.

  2. #2
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    Re: How can I get current time in C#.NET?

    seems weird,but I guess when you declare
    Code:
    DateTime time = DateTime.Now;
    the time is a global variable which is incorrectly being reseted to MinValue in other event procedures.

  3. #3
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    Re: How can I get current time in C#.NET?

    Thank you.

    However, I did not declare it as global variable, just local variable. Weird, Uh?

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: How can I get current time in C#.NET?

    You aren't doing something like this are you by any chance ?

    Code:
    private void Function1()
    {
        DateTime time = DateTime.Now;
    }
    
    private void Function2()
    {
        DateTime time; // expecting it to be set by function 1
    }
    Setting "time" in function 1 won't set it in function 2 : each value-type variable has a 'scope' i.e. it is limited to whichever blocks {} it is declared in.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center