【www.gdgbn.com--时间日期】

有一个textbox1和textbox2 和一个button
要求textbox1输入毫秒数以后。按button  textbox2显示时间
比如textbox1输入1248671343262 按button以后textbox2显示类似mon jul 27 13:09:03 cst 2009这样的时间

你所说的毫秒数1248671343262是将datetime对象通过方法tofiletimeutc或tofiletime转换成的windows文件时间(一个 64 位值),它表示自协调世界时 (utc) 公元 (c.e.) 1601 年 1 月 1 日午夜 12:00 以来已经过的间隔数(以 100 纳秒为一个间隔)。windows 使用文件时间记录应用程序创建、访问或写入文件的时间。
首先将其反向转换成datetime对象:
datetime dt = datetime.fromfiletimeutc(1248671343262);
也可以将其先转换成long ticks(刻度为100纳秒),
long ticks = 1248671343262 / 100;
datetime dt = new datetime(ticks);
再通过格式转换:
dt.getdatetimeformats("r")[0].tostring();
//tue, 02 jan 1601 10:41:07 gmt
再看看gmt和cst之间的区别,做些调整。

下面看unix时间戳与c# datetime时间类型互换

///


/// method for converting a unix timestamp to a regular
/// system.datetime value (and also to the current local time)
///

/// value to be converted
/// converted datetime in string format
private static datetime converttimestamp(double timestamp)
{
    //create a new datetime value based on the unix epoch
    datetime converted = new datetime(1970, 1, 1, 0, 0, 0, 0);

    //add the timestamp to the value
    datetime newdatetime = converted.addseconds(timestamp);

    //return the value in string format
    return newdatetime.tolocaltime();
}


///


/// method for converting a system.datetime value to a unix timestamp
///

/// date to convert
///
private double converttotimestamp(datetime value)
{
    //create timespan by subtracting the value provided from
    //the unix epoch
    timespan span = (value - new datetime(1970, 1, 1, 0, 0, 0, 0).tolocaltime());

    //return the total seconds (which is a unix timestamp)
    return (double)span.totalseconds;
}

本文来源:http://www.gdgbn.com/wangyetexiao/29010/