Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Get And Set The System Date And Time

C# code snippet that uses unmanaged code to retrieve the current date and time of the Windows operating system, and also sets it to the specified values.

On Wednesday, April 23rd 2008 at 11:23 PM
By Andrew Pociu (View Profile)
****-   (Rated 4 with 21 votes)
Contextual Ads
More C# Resources
Advertisement
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Windows.Forms;
  6. using System.Runtime.InteropServices;
  7.  
  8. namespace Sample
  9. {
  10.     public partial class Form1 : Form
  11.     {
  12.         public Form1()
  13.         {
  14.             InitializeComponent();
  15.         }
  16.  
  17.         public struct SystemTime
  18.         {
  19.             public ushort Year;
  20.             public ushort Month;
  21.             public ushort DayOfWeek;
  22.             public ushort Day;
  23.             public ushort Hour;
  24.             public ushort Minute;
  25.             public ushort Second;
  26.             public ushort Millisecond;
  27.         };
  28.  
  29.         [DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)]
  30.         public extern static void Win32GetSystemTime(ref SystemTime sysTime);
  31.  
  32.         [DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
  33.         public extern static bool Win32SetSystemTime(ref SystemTime sysTime);
  34.  
  35.         private void button1_Click(object sender, EventArgs e)
  36.         {
  37.             // Set system date and time
  38.             SystemTime updatedTime = new SystemTime();
  39.             updatedTime.Year = (ushort)2008;
  40.             updatedTime.Month = (ushort)4;
  41.             updatedTime.Day = (ushort)23;
  42.             // UTC time; it will be modified according to the regional settings of the target computer so the actual hour might differ
  43.             updatedTime.Hour = (ushort)10;
  44.             updatedTime.Minute = (ushort)0;
  45.             updatedTime.Second = (ushort)0;
  46.             // Call the unmanaged function that sets the new date and time instantly
  47.             Win32SetSystemTime(ref updatedTime);
  48.  
  49.             // Retrieve the current system date and time
  50.             SystemTime currTime = new SystemTime();
  51.             Win32GetSystemTime(ref currTime);
  52.             // You can now use the struct to retrieve the date and time
  53.             MessageBox.Show("It's " + currTime.Hour + " o'clock. Do you know where your C# code is?");
  54.         }
  55.     }
  56. }
  57.  
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this code snippet
Comment Current Comments
by Masud on Monday, July 14th 2008 at 07:00 AM

How can i get real system time?

by Ibrar on Tuesday, July 15th 2008 at 04:31 AM

Is there a way of getting system time in real time so that it runs like in its own thread. What ways are there to achieve this? It seems threads are the only way to go ...

by kasinathan on Tuesday, November 11th 2008 at 10:39 AM

hello !
any one can help me?
how can i get the system time only not date .. then i want to assign to the lable?

plz any one can help using c# ASP.net

by mahesh on Monday, June 29th 2009 at 08:06 PM

Hi, In this sample, which format the time display? When I am trying this at 9'o clock it shows 1. What is wrong. And one If you want show date please get the value from that structure.

by c on Thursday, August 20th 2009 at 02:51 AM

hi kasinathan

string str=DateTime.Now.ToShortTimeString();
yourLable.Text=str;

by monali on Friday, September 4th 2009 at 02:51 PM

Hi all,
The code looks good but when I am trying to run that it is giving me error in hours. I mean when I give 1 in hr place its taking 5pm when I give 14 it taking 6 am and so on. Can any body help in where it is going wrong. I need that for my project.

thanks in advanse.

by Omid on Thursday, January 7th 2010 at 04:08 PM

hi
thats not work in windows 7 !!!
how we can do that ?

by Omid on Thursday, January 7th 2010 at 04:13 PM

hi
thats not work in windows 7 !!!
how we can do that ?

by Omid on Thursday, January 7th 2010 at 04:15 PM

hi
thats not work in windows 7 !!!
how we can do that ?

by Indi on Sunday, April 11th 2010 at 10:06 PM

This does not work properly, when system time is in the hour of 12:--PM

by Prithivi on Monday, April 26th 2010 at 11:46 AM

i want to retrieve the date from already stored in sqlserver database and subract retrieve date from today date in asp.net using c#.

by Neha on Thursday, May 13th 2010 at 02:34 AM

Hi,
I tried it. It just dispalays the output form without chnaging the system time. I have Vista installed on my system.

Does this work in windows vista??
Please help

by Mohsen on Tuesday, November 23rd 2010 at 03:45 AM

begir

by Amir Sayani on Saturday, December 25th 2010 at 01:51 AM

Not working in windows 7

by qBert on Wednesday, January 12th 2011 at 02:34 AM

Hello,
I have windows 7 and this sample work fine. I have another prolem, i split "SystemTime" in 2 pieces (SystemTime and SystemData), i need toinsert 'Date' and 'Time' from textbox (or nomatter what)..and don't work. Guess that i mistake at the part. Can someone help me? tks

by Ronelsax on Tuesday, January 18th 2011 at 03:04 PM

For those that are having problems setting the time (i.e. setting one hour and getting a different one), this means you have to either compensate your time zone (i.e. for PST-8 you'll have to subtract eight hours) or use the "GetLocalTime" and "SetLocalTime" unmanaged versions.

by Ronelsax on Tuesday, January 18th 2011 at 03:04 PM

For those that are having problems setting the time (i.e. setting one hour and getting a different one), this means you have to either compensate your time zone (i.e. for PST-8 you'll have to subtract eight hours) or use the "GetLocalTime" and "SetLocalTime" unmanaged versions.

by kokul on Monday, July 25th 2011 at 05:13 AM

hi C
thank you very much.. you solved my problem relevant to get current time

by 1 on Tuesday, October 18th 2011 at 07:28 AM

1

by GenF20 Plus on Monday, December 19th 2011 at 05:25 PM

This is a great trick and I didn’t know you can use C# to obtain the info of your computer

by Human Growth Hormone on Monday, December 19th 2011 at 05:29 PM

This is a great trick and I didn’t know you can use C# to obtain the info of your computer

by Phen375 on Monday, December 19th 2011 at 05:34 PM

Does this trick work for Mac too?

by GenFX on Monday, December 19th 2011 at 05:37 PM

Does this trick work for Mac too?

by Teeth Whitening Reviews on Monday, December 19th 2011 at 05:42 PM

I needed to create you this tiny word so as to give thanks once again for your pleasant techniques you have provided on this site. This has been shockingly open-handed with people like you to supply openly what exactly a number of people would've offered for an e book to help make some profit for themselves, precisely now that you could possibly have done it in case you desired.

by Nuratrim on Monday, December 19th 2011 at 05:43 PM

I needed to create you this tiny word so as to give thanks once again for your pleasant techniques you have provided on this site. This has been shockingly open-handed with people like you to supply openly what exactly a number of people would've offered for an e book to help make some profit for themselves, precisely now that you could possibly have done it in case you desired.

by Ultimate Maqui Berry on Monday, December 19th 2011 at 05:47 PM

Those concepts additionally worked as a good way to recognize that other people have the same desire really like my own to see significantly more in regard to this issue. Certainly there are numerous more pleasurable opportunities up front for people who looked over your blog.

by Best Diet Pills on Monday, December 19th 2011 at 05:48 PM

Those concepts additionally worked as a good way to recognize that other people have the same desire really like my own to see significantly more in regard to this issue. Certainly there are numerous more pleasurable opportunities up front for people who looked over your blog.

by Dental White on Monday, December 19th 2011 at 05:50 PM

I have to express my thanks to the writer just for rescuing me from such a difficulty. Because of browsing through the online world and seeing views which were not helpful, I believed my life was done. Existing without the presence of solutions to the problems you've fixed as a result of your main write-up is a crucial case, and the kind which could have in a wrong way affected my entire career if I hadn't discovered your website.

by Pure Acai Berry Max on Monday, December 19th 2011 at 05:53 PM

I have to express my thanks to the writer just for rescuing me from such a difficulty. Because of browsing through the online world and seeing views which were not helpful, I believed my life was done. Existing without the presence of solutions to the problems you've fixed as a result of your main write-up is a crucial case, and the kind which could have in a wrong way affected my entire career if I hadn't discovered your website.

by Leon on Tuesday, January 24th 2012 at 05:52 AM

Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view

by dragon naturally spea on Tuesday, October 2nd 2012 at 01:59 AM

publish your web page with some innotative concepts with the code. Will make this page a world of good.

by invisible fence raleigh nc on Thursday, November 8th 2012 at 02:37 AM

invisible fence raleigh nc

by anuva on Saturday, February 16th 2013 at 06:45 AM

http://www.youtube.com/watch?v=AK8aQcka0b0

by anuva on Saturday, February 16th 2013 at 06:48 AM

[youtube http://www.youtube.com/watch?v=AK8aQcka0b0]

by insanity Schedule on Wednesday, February 20th 2013 at 09:26 AM

http://www.youtube.com/user/insanityschedule

by Akwin Dhas on Tuesday, May 6th 2014 at 07:09 AM

plz try to write the perfect code this code is not working correct way


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs C# Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources