namespace ConsoleApplication1
{
///
/// unix与windowns日期格式互换
/// zhangyong 2012/03/07
/// class Program
{
private static readonly string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
static void Main(string[] args)
{
string DataStr = time;
Program p = new Program();
string tempTime = p.ConvertToUnix(DataStr);
string ssd = p.ConvertToWin(tempTime);
}
///
/// 将nuix中的日期格式转换成正常日期格式,前提传入的格式正确
/// ///
传入的时间戳
///
public String ConvertToWin(String timestampString)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1));
long lTime = long.Parse(timestampString + "0000000");
TimeSpan toNow = new TimeSpan(lTime);
DateTime dtResult = dtStart.Add(toNow);
return dtResult.ToString("yyyy-MM-dd HH:mm:ss");
}
///
/// 将正常的日期转换成unix日期时间戳格式
/// ///
正常日期转换成的字符串格式如:yyyy-MM-dd HH:mm:ss
///
unix时间 public string ConvertToUnix(string dateTime)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
DateTime dtNow = DateTime.Parse(dateTime);
TimeSpan toNow = dtNow.Subtract(dtStart);
string timeStamp = toNow.Ticks.ToString();
timeStamp = timeStamp.Substring(0, timeStamp.Length - 7);
return timeStamp;