Click here to Skip to main content
15,906,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I am New to .net How to set Global DateTime in my Application
Posted

If you just want to get the system time -> DateTime.Now is your friend, but If you want to SET the system clock, you can do it like this: (I use this methods in a .NET CF application, the example code was taken from MSDN)

using System;
using System.Runtime.InteropServices;
namespace SetSystemTime
{
    class Program
    {
        [DllImport("kernel32.dll")] // for Windows CE/.NET CF use [DllImport("coredll.dll")]
        private extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);
        [DllImport("kernel32.dll")] // for Windows CE/.NET CF use [DllImport("coredll.dll")]
        private extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime);
        [StructLayout(LayoutKind.Sequential)]
        private struct SYSTEMTIME
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMilliseconds;
        }
        static void Main(string[] args)
        {
            // Get system time - this can be done without P/Invoke using DateTime.Now
            SYSTEMTIME timeNow = new SYSTEMTIME();
            GetSystemTime(ref timeNow);
            Console.WriteLine("Now: {0}:{1}:{2}", timeNow.wHour, timeNow.wMinute, timeNow.wSecond);
            // Set system clock
            SYSTEMTIME timeNew = new SYSTEMTIME();
            GetSystemTime(ref timeNew);
            // ...  ahead one hour
            timeNew.wHour = (ushort)(timeNew.wHour + 1 % 24);
            SetSystemTime(ref timeNew);
            Console.WriteLine("New: {0}:{1}:{2}", timeNew.wHour, timeNew.wMinute, timeNew.wSecond);
        }
    }
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Mar-11 11:19am    
Very good, a 5.
--SA
Dalek Dave 4-Mar-11 4:17am    
Excellent Answer.
Based on the approach of johannesnestler[^], I'm using this utility class with DateTime support:
C#
using System;
using System.Runtime.InteropServices;

namespace SystemTimeTool
{

  // ------------------------------------------------------------------------
  [CLSCompliant( false )]
  public static class SystemTime
  {

    // ----------------------------------------------------------------------
    public struct SystemTimeInfo
    {
      public ushort Year;
      public ushort Month;
      public ushort DayOfWeek;
      public ushort Day;
      public ushort Hour;
      public ushort Minute;
      public ushort Second;
      public ushort Millisecond;
    } // struct SystemTimeInfo

    // ----------------------------------------------------------------------
    [DllImport( "kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true )]
    public extern static void Win32GetSystemTime( ref SystemTimeInfo systemTimeInfo );

    // ----------------------------------------------------------------------
    [DllImport( "kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true )]
    public extern static bool Win32SetSystemTime( ref SystemTimeInfo systemTimeInfo );

    // ----------------------------------------------------------------------
    public static DateTime GetSystemTime()
    {
      SystemTimeInfo systemTimeInfo = new SystemTimeInfo();
      Win32GetSystemTime( ref systemTimeInfo );

      return new DateTime( 
        systemTimeInfo.Year,
        systemTimeInfo.Month,
        systemTimeInfo.Day,
        systemTimeInfo.Hour,
        systemTimeInfo.Minute,
        systemTimeInfo.Second,
        systemTimeInfo.Millisecond );
    } // GetSystemTime

    // ----------------------------------------------------------------------
    public static bool SetSystemTime( DateTime dateTime )
    {
      SystemTimeInfo systemTimeInfo;
      systemTimeInfo.Year = (ushort)dateTime.Year;
      systemTimeInfo.Month = (ushort)dateTime.Month;
      systemTimeInfo.DayOfWeek = (ushort)dateTime.DayOfWeek;
      systemTimeInfo.Day = (ushort)dateTime.Day;
      systemTimeInfo.Hour = (ushort)dateTime.Hour;
      systemTimeInfo.Minute = (ushort)dateTime.Minute;
      systemTimeInfo.Second = (ushort)dateTime.Second;
      systemTimeInfo.Millisecond = (ushort)dateTime.Millisecond;

      return Win32SetSystemTime( ref systemTimeInfo );
    } // SetSystemTime

  } // class SystemTime

} // namespace SystemTimeTool
 
Share this answer
 
Comments
johannesnestler 27-Mar-12 11:49am    
5ed - good wrapper
Are you asking for a global variable that holds the current date/time?

Any time you need the current date/time, just do this:

C#
DateTime now = DateTime.Now;
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Mar-11 11:20am    
There is no "set" in your answer...
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900