Results 1 to 5 of 5

Thread: C# : Setting Date & time

  1. #1
    Join Date
    May 2008
    Posts
    27

    C# : Setting Date & time

    Hi,
    I want to set the date & time in Pocket PC using C# windows form application with Compact framework.
    Actually I am trying to synchronize the date & time with the db server.
    Need some guidance on how to do this?

  2. #2
    Join Date
    May 2008
    Posts
    26

    Re: C# : Setting Date & time

    Try this SetLocalTime or SetSystemTime.

    http://opennetcf.org/winapi.asp for the implementations.

    I hope this helps you!

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: C# : Setting Date & time

    Hey I have this code that may help you understand the function.

    Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.Windows.Forms;
    using System.Data;
    using System.Runtime.InteropServices;
    using System.Diagnostics;
    
    namespace SetSystemTime
    {
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.Panel panel2;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.Label label5;
    private System.Windows.Forms.Label label6;
    private System.Windows.Forms.Label lblOriginalLocal;
    private System.Windows.Forms.Label lblOriginalUtc;
    private System.Windows.Forms.Label lblNewLocal;
    private System.Windows.Forms.Label lblNewUtc;
    private System.Windows.Forms.MainMenu mainMenu1;
    
    public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();
    
    //
    // TODO: Add any constructor code after InitializeComponent call
    //
    }
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    base.Dispose( disposing );
    }
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.mainMenu1 = new System.Windows.Forms.MainMenu();
    this.button1 = new System.Windows.Forms.Button();
    this.panel1 = new System.Windows.Forms.Panel();
    this.label1 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.label3 = new System.Windows.Forms.Label();
    this.panel2 = new System.Windows.Forms.Panel();
    this.label4 = new System.Windows.Forms.Label();
    this.label5 = new System.Windows.Forms.Label();
    this.label6 = new System.Windows.Forms.Label();
    this.lblOriginalLocal = new System.Windows.Forms.Label();
    this.lblOriginalUtc = new System.Windows.Forms.Label();
    this.lblNewLocal = new System.Windows.Forms.Label();
    this.lblNewUtc = new System.Windows.Forms.Label();
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(160, 240);
    this.button1.Text = "button1";
    this.button1.Click += new
    System.EventHandler(this.button1_Click);
    //
    // panel1
    //
    this.panel1.BackColor = System.Drawing.Color.Silver;
    this.panel1.Controls.Add(this.lblOriginalUtc);
    this.panel1.Controls.Add(this.lblOriginalLocal);
    this.panel1.Controls.Add(this.label3);
    this.panel1.Controls.Add(this.label2);
    this.panel1.Controls.Add(this.label1);
    this.panel1.Location = new System.Drawing.Point(0, 8);
    this.panel1.Size = new System.Drawing.Size(240, 100);
    //
    // label1
    //
    this.label1.Font = new System.Drawing.Font("Tahoma", 9F,
    System.Drawing.FontStyle.Regular);
    this.label1.Location = new System.Drawing.Point(8, 32);
    this.label1.Size = new System.Drawing.Size(136, 20);
    this.label1.Text = "Local";
    //
    // label2
    //
    this.label2.Font = new System.Drawing.Font("Tahoma", 9F,
    System.Drawing.FontStyle.Regular);
    this.label2.Location = new System.Drawing.Point(8, 64);
    this.label2.Size = new System.Drawing.Size(136, 20);
    this.label2.Text = "UTC";
    //
    // label3
    //
    this.label3.Location = new System.Drawing.Point(0, 8);
    this.label3.Text = "Original Time";
    //
    // panel2
    //
    this.panel2.BackColor = System.Drawing.Color.Silver;
    this.panel2.Controls.Add(this.lblNewUtc);
    this.panel2.Controls.Add(this.lblNewLocal);
    this.panel2.Controls.Add(this.label4);
    this.panel2.Controls.Add(this.label5);
    this.panel2.Controls.Add(this.label6);
    this.panel2.Location = new System.Drawing.Point(0, 120);
    this.panel2.Size = new System.Drawing.Size(240, 100);
    //
    // label4
    //
    this.label4.Location = new System.Drawing.Point(0, 8);
    this.label4.Text = "New Time";
    //
    // label5
    //
    this.label5.Font = new System.Drawing.Font("Tahoma", 9F,
    System.Drawing.FontStyle.Regular);
    this.label5.Location = new System.Drawing.Point(8, 64);
    this.label5.Size = new System.Drawing.Size(136, 20);
    this.label5.Text = "UTC";
    //
    // label6
    //
    this.label6.Font = new System.Drawing.Font("Tahoma", 9F,
    System.Drawing.FontStyle.Regular);
    this.label6.Location = new System.Drawing.Point(8, 32);
    this.label6.Size = new System.Drawing.Size(136, 20);
    this.label6.Text = "Local";
    //
    // lblOriginalLocal
    //
    this.lblOriginalLocal.Location = new System.Drawing.Point(152,
    32);
    this.lblOriginalLocal.Size = new System.Drawing.Size(80, 20);
    //
    // lblOriginalUtc
    //
    this.lblOriginalUtc.Font = new System.Drawing.Font("Tahoma", 9F,
    System.Drawing.FontStyle.Regular);
    this.lblOriginalUtc.Location = new System.Drawing.Point(152,
    64);
    this.lblOriginalUtc.Size = new System.Drawing.Size(80, 20);
    //
    // lblNewLocal
    //
    this.lblNewLocal.Font = new System.Drawing.Font("Tahoma", 9F,
    System.Drawing.FontStyle.Regular);
    this.lblNewLocal.Location = new System.Drawing.Point(152, 32);
    this.lblNewLocal.Size = new System.Drawing.Size(80, 20);
    //
    // lblNewUtc
    //
    this.lblNewUtc.Font = new System.Drawing.Font("Tahoma", 9F,
    System.Drawing.FontStyle.Regular);
    this.lblNewUtc.Location = new System.Drawing.Point(152, 64);
    this.lblNewUtc.Size = new System.Drawing.Size(80, 20);
    //
    // Form1
    //
    this.Controls.Add(this.panel2);
    this.Controls.Add(this.panel1);
    this.Controls.Add(this.button1);
    this.Menu = this.mainMenu1;
    this.Text = "Form1";
    
    }
    #endregion
    
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    
    static void Main()
    {
    Application.Run(new Form1());
    }
    public struct SystemTime
    {
    public ushort Year;
    public ushort Month;
    public ushort DayOfWeek;
    public ushort Day;
    public ushort Hour;
    public ushort Minute;
    public ushort Second;
    public ushort Millisecond;
    };
    
    [DllImport("kernel32.dll", EntryPoint="GetSystemTime",
    SetLastError=true)]
    public extern static void Win32GetSystemTime(ref SystemTime st);
    
    [DllImport("coredll.dll", EntryPoint="GetSystemTime",
    SetLastError=true)]
    public extern static void WinCEGetSystemTime(ref SystemTime st);
    
    [DllImport("kernel32.dll", EntryPoint="SetSystemTime",
    SetLastError=true)]
    public extern static bool Win32SetSystemTime(ref SystemTime st);
    
    [DllImport("coredll.dll", EntryPoint="SetSystemTime",
    SetLastError=true)]
    public extern static bool WinCESetSystemTime(ref SystemTime st);
    
    private void button1_Click(object sender, System.EventArgs e)
    {
    
    try
    {
    SystemTime st = new SystemTime();
    
    //WinCEGetSystemTime(ref st);
    
    //st.Hour = 1;
    //st.Minute = 30;
    
    DateTime dt = DateTime.UtcNow;
    this.lblOriginalUtc.Text = dt.ToShortTimeString();
    st.Year = (ushort)dt.Year;
    st.Month = (ushort)dt.Month;
    st.Day = (ushort)dt.Day;
    st.DayOfWeek = (ushort)dt.DayOfWeek;
    st.Hour = (ushort)((dt.Hour + 1) % 24);
    st.Minute = (ushort)dt.Minute;
    st.Second = 59;
    st.Millisecond = (ushort)dt.Millisecond;
    this.lblOriginalLocal.Text =
    dt.ToLocalTime().ToShortTimeString();
    
    bool result = WinCESetSystemTime(ref st);
    if (true != result)
    {
    int error = Marshal.GetLastWin32Error();
    Debug.WriteLine(String.Format("SetSystemTime failed:
    {0:X8}", error));
    }
    dt = DateTime.UtcNow;
    this.lblNewUtc.Text = dt.ToShortTimeString();
    this.lblNewLocal.Text =
    dt.ToLocalTime().ToShortTimeString();
    }
    catch (Exception x)
    {
    Debug.WriteLine(x);
    }
    }
    }
    }

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: C# : Setting Date & time

    Well in the forum we have used one of the many methods available to set the date & time automatically to the server.
    You need to find out how to store it to the server.

  5. #5
    Join Date
    Jan 2009
    Posts
    44

    Re: C# : Setting Date & time

    If the value is always going to be the current date & time, set a default on
    the column of getdate()

Similar Threads

  1. Replies: 2
    Last Post: 29-11-2011, 09:29 AM
  2. Replies: 8
    Last Post: 19-10-2011, 05:08 AM
  3. setting time and date in micro dv camcorder muvi
    By paul777 in forum Portable Devices
    Replies: 3
    Last Post: 25-05-2011, 11:55 PM
  4. Date & time Conversion PST Pacific Standard Time to GMT
    By Austinage in forum Software Development
    Replies: 2
    Last Post: 30-07-2009, 05:38 PM
  5. Setting my PDC Emulator to time sync to external time source
    By aja44 in forum Windows Server Help
    Replies: 1
    Last Post: 17-05-2008, 07:38 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,256,285.36072 seconds with 17 queries