请问1242060640这样一个数字能否转换为2009-05-12这样的日期?

解决方案 »

  1.   

    没看懂1242060640和2009-05-12之间是什么关系要转换可以新建一个DateTime的变量,然后调用合适的构造函数。
    DateTime构造函数有十几个重载,根据需要选择一个就可以了。
    DateTime t = new DateTime(xxx);
      

  2.   

    mysql> select FROM_UNIXTIME(1242060640);
    +---------------------------+
    | FROM_UNIXTIME(1242060640) |
    +---------------------------+
    | 2009-05-12 00:50:40       |
    +---------------------------+
    1 row in set (0.00 sec)
      

  3.   

    Convert.ToDateTime(日期).ToString("yyyy-MM-dd")
      

  4.   

    try...string test = "1242060640";
    DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
    long lTime = -1;
    long.TryParse(test + "0000000", out lTime);
    if (lTime == 0 || lTime == -1)
        richTextBox2.Text = "待转换数据不是有效的UNIX时间戳!";
    TimeSpan toNow = new TimeSpan(lTime);
    DateTime dtResult = dtStart.Add(toNow);
    richTextBox2.Text = dtResult.ToString("yyyy-MM-dd HH:mm:ss");  //2009-05-12 00:50:40
      

  5.   

    [code=C#
    Convert.ToDateTime(1242060640).ToString("yyyy-MM-dd HH:mm:ss")
    [/code]
      

  6.   

      将数字转换为日期格式时,可以首先声明DateTime类的一个实例对象,并将要转换为日期格式的数字赋值给该对象,然后调用该对象的ToLongDateString方法或ToShortDateString方法进行格式转换。将数字转换为日期格式的关键代码如下:DateTime mydatetime = new DateTime(Convert.ToInt32(comboBox1.Text), Convert.ToInt32(comboBox2.Text), Convert.ToInt32(comboBox3.Text)); 
    MessageBox.Show("日期为:" + mydatetime.ToLongDateString(), "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      
      

  7.   

    DateTime mydatetime = new DateTime(1999,12,02);
                Console.WriteLine(mydatetime.ToLongDateString());
    结果显示:1999年12月2日
      

  8.   

    那如果反向该怎么操作呢?比如把yyyy-MM-dd HH:mm:ss格式转换为支持mysql中支持的int类型。
    5楼大哥的那段代码反向就能行吗?
      

  9.   

       string _TextString = "1242060640";
                DateTime _LoadTime = new DateTime(1970, 1, 1,8,0,0);
                _LoadTime = _LoadTime.AddSeconds(double.Parse(_TextString));
      

  10.   

    终于写出来了,谢谢各位兄弟的帮助。        private string DateDiff(DateTime DateTime1, DateTime DateTime2)
            {
                string dateDiff = null;
                TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
                TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
                TimeSpan ts = ts1.Subtract(ts2).Duration();
                dateDiff = ts.TotalSeconds.ToString();
                return dateDiff;
            }        private void button2_Click(object sender, EventArgs e)
            {
                textBox1.Text = DateDiff(Convert.ToDateTime( DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")), new DateTime(1970, 1, 1, 0, 0, 0));
            }
      

  11.   

      .net采用新的日期方法,就是使用Int64来代替Double类型储存时间。但仍然提供了转换为老类型日期的方法,就是DataTime的ToOADate和FormOADate方法。你在MSDN中输入这些关键字,就能很快找到使用方法。