时间与时间戳互转的c#语法
时间戳转换为时间
源代码:
using System;
using System.Collections.Generic;
using SpiderInterface;
class LocoyCode{
/// <summary>
/// 执行方法,不能修改类和方法名称。
/// </summary>
/// <param name="content">标签内容</param>
/// <param name="response">页面响应,包含了Url、原始Html等属性</param>
/// <returns>返回处理后的标签内容</returns>
public string Run(string content,ResponseEntry response){
// 在这里编写处理代码
DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
// 时间戳默认为10位,如果是13位,那么需要将下面的 0000000 改为 0000
long lTime = long.Parse(content + "0000000");
TimeSpan toNow = new TimeSpan(lTime);
DateTime newTime = dateTimeStart.Add(toNow);
content = newTime.ToString("yyyy-MM-dd HH:mm:ss");
return content;
}
}时间转换为时间戳
源代码:
using System;
using System.Collections.Generic;
using SpiderInterface;
class LocoyCode{
/// <summary>
/// 执行方法,不能修改类和方法名称。
/// </summary>
/// <param name="content">标签内容</param>
/// <param name="response">页面响应,包含了Url、原始Html等属性</param>
/// <returns>返回处理后的标签内容</returns>
public string Run(string content,ResponseEntry response){
// 在这里编写处理代码
string dt = "yyyy-MM-dd HH:mm:ss";
DateTime time = DateTime.ParseExact(content, dt, System.Globalization.CultureInfo.CurrentCulture);
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
// 这里除10000000将时间戳调整为10位,如果要保留13位,那么将 10000000 改为 10000
long t = (time.Ticks - startTime.Ticks) / 10000000;
content = t.ToString();
return content;
}
}