How does C # modify computer time dates and formats? Using the WINDOWAPI function can be done

Source: Internet
Author: User
Tags datetime

C # Use the WINDOWAPI function to modify the computer's date and time and format, the code snippet is as follows, have a friend to take it.

The code is as follows Copy Code
Public Class Setcomputerdatetime

{

[DllImport ("kernel32.dll", EntryPoint = "GetSystemDefaultLCID")]
public static extern int getsystemdefaultlcid ();

[DllImport ("kernel32.dll", EntryPoint = "Setlocaleinfoa")]
public static extern int SetLocaleInfo (int Locale, int lctype, string lplcdata);

public const int locale_slongdate = 0x20;
public const int locale_sshortdate = 0x1F;
public const int locale_stime = 0x1E;

[DllImportAttribute ("Kernel32.dll")]
public static extern void Setlocaltime (SystemTime st);

public ushort Wyear;

public ushort Wmonth;

public ushort Wdayofweek;
public ushort Wday;
public ushort Whour;
public ushort Wminute;
public ushort Wsecond;
public ushort Wmilliseconds;

Public Setcomputerdatetimeformat ()

{

}

public void Setdatetimeformat ()

{

Try
{
int x = GetSystemDefaultLCID ();
SetLocaleInfo (x, Locale_sshortdate, "yyyy/mm/dd"); Short Date format
SetLocaleInfo (x, Locale_slongdate, "yyyy year mm month DD Day"); Long Date format
SetLocaleInfo (x, Locale_stime, "HH:mm:ss tt"); Time format
}
catch (Exception ex)

{

Console.WriteLine (ex);
}

}

public void SetDateTime (datetime datetime)

{

Try
{
SystemTime mysystemtime = new SystemTime ();
MySystemTime. Wyear = (ushort) systemdatetime.year;//year
MySystemTime. Wmonth = (ushort) systemdatetime.month;//Month
MySystemTime. Wday = (ushort) systemdatetime.day;//Day
MySystemTime. Whour = (ushort) systemdatetime.hour;//
MySystemTime. Wminute = (ushort) systemdatetime.minute;//min.
MySystemTime. Wsecond = (ushort) systemdatetime.second;//sec
Setlocaltime (MySystemTime);
}
catch (Exception ex)
{
Console.WriteLine (ex);
}

}

}

[StructLayoutAttribute (LayoutKind.Sequential)]
public class 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;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.