一个UnversalTime与本地时间相互转换的工具

这是个小例侱,用于UnversalTime与本地时间的相互转换,最底下的两个空格时用来计算离现在X小时Y分前的时间的。本例程用Visual Studio 2010开发。.NET Framework 4.0。由于嫌麻烦我把修改DateTimePicker值的方法写成了一个事件。这个例程也可以算是个自定义事件的例程吧。

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

namespace UTC_LocalTime
{
    public partial class Form1 : Form
    {
        private DatePickUnit dpu = new DatePickUnit();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string FormatString = "yyyy年M月d日 H:mm"; //y年,M月,d日,H24小时制,h12小时制,s秒
            dateTimePicker1.CustomFormat = FormatString;
            dateTimePicker2.CustomFormat = FormatString;
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker2.Format = DateTimePickerFormat.Custom;
            dpu.UniversalTimePicker = dateTimePicker1;
            dpu.LocalTimePicker = dateTimePicker2;
            dpu.TimeChange += new DatePickUnit.TimeChangeHandler(dpu_TimeChange);
            dpu.ChangeTime(DateTime.Now,false);
        }

        void dpu_TimeChange(DateTimePicker UniversalTimePicker, DateTimePicker LocalTimePicker, DateTime TimeValue, bool IsUniversalTimeChanged)
        {
            if (IsUniversalTimeChanged)
            {
                LocalTimePicker.Value = TimeValue;
            }
            else
            {
                UniversalTimePicker.Value = TimeValue.ToUniversalTime();
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            dpu.ChangeTime(dateTimePicker1.Value.ToLocalTime(), true);
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            dpu.ChangeTime(dateTimePicker2.Value, false);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strHours = textBox1.Text;
            string strMinutes = textBox2.Text;
            int hours = 0;
            int minutes = 0;
            hours = Convert(strHours,true);
            minutes = Convert(strMinutes,false);
            DateTime current = DateTime.Now;
            current = current.AddHours(-1 * hours);
            current = current.AddMinutes(-1 * minutes);
            dpu.ChangeTime(current, false);
            textBox1.Text = "";
            textBox2.Text = "";
        }

        private int Convert(string strValue,bool IsHour)
        {
            int result = 0;
            string strTitle = string.Empty;
            try
            {
                if (strValue.Length > 0)
                {
                    result = int.Parse(strValue);
                }
            }
            catch (Exception ex)
            {
                if (ex is FormatException)
                {
                    if (IsHour)
                    {
                        strTitle = "小时";
                    }
                    else
                    {
                        strTitle = "分钟";
                    }
                    MessageBox.Show(strTitle + "数字格式错误,请重新设定!", strTitle + "格式错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                }
            }
            return result;
        }
    }

    internal class DatePickUnit
    {
        public DateTimePicker UniversalTimePicker{set;get;}
        public DateTimePicker LocalTimePicker{set;get;}
        public delegate void TimeChangeHandler(DateTimePicker UniversalTimePicker,DateTimePicker LocalTimePicker,DateTime TimeValue,bool IsUniversalTimeChanged);
        public event TimeChangeHandler TimeChange;

        public void ChangeTime(DateTime time,bool IsUniversalTimeChanged)
        {
            TimeChange(UniversalTimePicker, LocalTimePicker, time,IsUniversalTimeChanged);
        }
    }
}

相关资源: http://download.csdn.net/detail/tx_officedev/3895915

你可能感兴趣的:(time)