简体   繁体   中英

How do i add more time to the datetime.now parse it then reset back to the original datetime.now?

This is the code in Form1:

private DateTime dt;
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        internal static extern bool SetLocalTime(ref SYSTEMTIME lpSystemTime);

        [StructLayout(LayoutKind.Sequential)]
        internal struct SYSTEMTIME
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;    // ignored for the SetLocalTime function
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMilliseconds;
        }

        private NotifyIcon notifyIcon1;

        public Form1()
        {
            InitializeComponent();

            notifyIcon1 = new NotifyIcon();
            SetBalloonTip();
            dt = GetNetworkTime();
            SYSTEMTIME time = new SYSTEMTIME();
            time.wDay = 1;
            time.wMonth = 5;
            time.wYear = 2011;
            time.wHour = 12;
            time.wMinute = 15;

            if (!SetLocalTime(ref time))
            {
                // The native function call failed, so throw an exception
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }

GetNetworkTime() return the current real time. What i want to do is to take the dt time and add to it another day for example.

time.wDay = 1;
time.wMonth = 5;
time.wYear = 2011;
time.wHour = 12;
time.wMinute = 15;

So in the end the variable time will return me the next day date and time and not 5/1/2011 12:15 PM

For exmaple if now the time and date are: 9/27/2013 3:39PM So now i want it to be 9/28/2013 4:39PM

Each time to add another day and another hour to the current time !

Then i want to wait 5 minutes and make that the time will be reset back to the original time automatic.

I will use later a button click event to make it each click to add to the current time a day and an hour and then a timer to wait 5 minutes and reset it back to the original time.

How can i do it ? I need to parse somehow the dt variable and add to it a day and an hour and use the SYSTEMTIME to set this as the current date and time and then again get the current date and time parse it and set it after 5 minutes.

dt = GetNetworkTime().AddDays(1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM