How to find the difference between system time and user input time in the format [hh:mm] in millisecond.

Recommended Answers

All 6 Replies

hi adobe71

it may be useful:
if we have a Common class such as this in c# :
//-------------------------------------------------

using System;

namespace MohitZist_Baygany
{
    class Common
    {
        static public int second = 0;
        static public int minute = 0;
        static public int hour = 0;
    }
}
//-------------------------------------------
add a timer control in form and in timer tick event :
private void timer1_Tick(object sender, EventArgs e)
{
            Common.second++;
            if (Common.second == 60)
            {
                Common.second = 0;
                Common.minute++;
            }
            if (Common.minute == 60)
            {
                Common.minute = 0;
                Common.hour++;
            }
            lableTimeLogined.Text = "system time login :" + 
                                            Common.hour +":" + 
                                            Common.minute + ":" + 
                                            Common.second; 
}

//--------------------------------------------------------------
we use Common class to use the public static variables , to use in any of places in our project.
change timer interval of timer to 1000;
for milisecond you must define an milisecond public static variable in Common and change interval of timer to 1 ; then in tick function of timer write Common.milisecond++ and change Common.second every 1000 Common.milisecond;

commented: N/A -2

How to find the difference between system time and user input time in the format [hh:mm] in millisecond.

DateTime userInput = DateTime.Parse("6/29/2010 5:2:3 PM");

 TimeSpan sp = DateTime.Now - userInput;

 double milis= sp.TotalMilliseconds;

u can use DATEDIFF function

How pls give small example,actually i want to develop alarm application usin thread.sleep(millisecond);

here is the syntax for dtatediff
datediff(DateInterval.Day, Date 1 , Date 2)

this will allow u upto seconds for microsecond u have to see and use ur own logic

it is not with thread but maybe useful : the name of project is alarm
Form1.Designer.cs

namespace alarm
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            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.components = new System.ComponentModel.Container();
            this.labelTime = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // labelTime
            // 
            this.labelTime.AutoSize = true;
            this.labelTime.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.labelTime.Location = new System.Drawing.Point(17, 18);
            this.labelTime.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0);
            this.labelTime.Name = "labelTime";
            this.labelTime.Size = new System.Drawing.Size(200, 31);
            this.labelTime.TabIndex = 0;
            this.labelTime.Text = "Time to alarm : ";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(17, 76);
            this.label2.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(173, 31);
            this.label2.TabIndex = 2;
            this.label2.Text = "Alarm Time : ";
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(200, 73);
            this.textBox1.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(118, 38);
            this.textBox1.TabIndex = 3;
            // 
            // timer1
            // 
            this.timer1.Enabled = true;
            this.timer1.Interval = 1;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(329, 73);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(66, 38);
            this.button1.TabIndex = 4;
            this.button1.Text = "Set";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(16F, 31F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(401, 141);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.labelTime);
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label labelTime;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Timer timer1;
        private System.Windows.Forms.Button button1;
    }
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace alarm
{
    public partial class Form1 : Form
    {
        int mili;
        bool seted;
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (seted)
            {
                mili--;
                labelTime.Text = "Time to alarm : " + mili;
                if (mili == 0)
                {
                    seted = false;
                    timer1.Stop();
                    MessageBox.Show("wake up");
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                seted = true;
                mili = int.Parse(textBox1.Text);
                timer1.Start();
            }
            catch (Exception ex)
            {
                timer1.Stop();
                MessageBox.Show(ex.Message,"error");
            }
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.