c#时间戳相互转换
/// <summary>
/// 获取时间戳
/// </summary>
/// <returns></returns>
public static string GetTimeSpan(System.DateTime time)
{
long ts = GetUnixTime(time);
return ts.ToString();
} /// <summary>
/// 将DateTime时间格式转换为Unix时间戳格式
/// </summary>
/// <param name="time">时间</param>
/// <returns>long</returns>
public static long GetUnixTime(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(, , , , , , ));
long t = (time.Ticks - startTime.Ticks) / ; //除10000调整为13位
return t;
}
/// <summary>
/// 将Unix时间戳转为C#时间格式
/// </summary>
/// <param name="timeSpan"></param>
/// <returns></returns>
public static DateTime GetDateTimeFromUnix(string timeSpan)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(, , ));
long lTime = long.Parse(timeSpan + "");
TimeSpan toNow = new TimeSpan(lTime);
return dtStart.Add(toNow);
}
/// <summary>
/// 创建时间
/// </summary>
public string createTime { get; set; }
public DateTime _createTime {
get
{
if (string.IsNullOrEmpty(this.createTime))
return DateTime.MinValue;
return TimerTool.GetDate
/// <summary>
/// 根据字节长度来截取字符串
/// </summary>
///<param name="origStr">原始字符串</param>
///<param name="length">提取前length个字节</param>
/// <returns></returns>
public static String CutByteString(string origStr, int length)
{
byte[] bytes = Encoding.Unicode.GetBytes(origStr);
int n = ; // 表示当前的字节数
int i = ; // 要截取的字节数
for (; i < bytes.GetLength() && n < length; i++)
{
// 偶数位置,如0、2、4等,为UCS2编码中两个字节的第一个字节
if (i % == )
{
n++; // 在UCS2第一个字节时n加1
}
else
{
// 当UCS2编码的第二个字节大于0时,该UCS2字符为汉字,一个汉字算两个字节
if (bytes[i] > )
{
n++;
}
}
}
// 如果i为奇数时,处理成偶数
if (i % == )
{
// 该UCS2字符是汉字时,去掉这个截一半的汉字 if (bytes[i] > )
i = i - ; // 该UCS2字符是字母或数字,则保留该字符
else
i = i + ;
}
return Encoding.Unicode.GetString(bytes, , i);
} var countlen = System.Text.Encoding.Default.GetByteCount(LocalDataManager.Instance.UserModel.student.name);
NameText.text = countlen > ? CutByteString(LocalDataManager.Instance.UserModel.student.name, ) : LocalDataManager.Instance.UserModel.student.name;
TimeFromUnix(this.createTime);
}
}
c#时间戳相互转换的更多相关文章
- js sql C#时间、时间戳相互转换
js. sql. C#时间.时间戳相互转换 //1.获取当前时间戳_c# ) / //2.时间戳->时间 C# DateTime b11 = GetTime(");//11位时间戳-& ...
- iOS标准时间与时间戳相互转换
iOS标准时间与时间戳相互转换 (2012-07-18 17:03:34) 转载▼ 标签: ios 时间戳 标准时间 格式 设置 转化 杂谈 分类: iPhone开发 设置时间显示格式: NS ...
- 字符串时间与Unix时间戳相互转换
字符串时间与Unix时间戳相互转换 /** * @Author: wangkun * @Date : 2016/1/21 13:43 * @Description : 字符串时间转换为Unix时间戳 ...
- python正常时间和unix时间戳相互转换的方法
python正常时间和unix时间戳相互转换的方法 本文实例讲述了python正常时间和unix时间戳相互转换的方法.分享给大家供大家参考.具体分析如下: 这段代码可以用来转换常规时间格式为unix时 ...
- Python3 日期与时间戳相互转换
开发中经常会对时间格式处理,对于时间数据,比如2019-02-28 10:23:29,有时需要日期与时间戳进行相互转换,在Python3中主要用到time模块,相关的函数如下: 其中unix_time ...
- jQuery添加添加时间与时间戳相互转换组件
时间与时间戳的格式相互转换(转换主要兼容ie8,ie8不支持new Date()) (function($) { $.extend({ myTime: { CurTime: function () { ...
- MySQL时间戳相互转换
mysql将时间戳转成常用时间格式 在mysql中,一个时间字段的存储类型是int(11),怎么转化成字符类型,比方存储为13270655222,需要转化为yyyy -mm-dd的形式. 使用 FRO ...
- iOS - 标准时间与时间戳相互转换
做倒计时后台传的不是时间戳,是时间然后需要与系统时间对比得出时间戳进行倒计时显示 #pragma mark -- 倒计时 // 倒计时时间 NSString* timeStr = task.recei ...
- JS获取当前时间及时间戳相互转换
1.获取当前时间的 时间戳 Date.parse(new Date()) 结果:1486347562000 2.获取当前 时间 new Date() 结果:Mon Feb 06 2017 10:19: ...
随机推荐
- Pycharm 解释器的快捷键
Ctrl+shift+Z 反撤销 Ctrl +/ 注释 ctrl+d 复制粘贴选中 Ctrl+y 删除默认一行 Ctrl+shift+r 全局搜索 Ctrl+alt+/ 代码整理 compare w ...
- SSM整合框架(基于IDEA的配置)
Pom文件 <?xml version="1.0" encoding="UTF-8"?><project xmlns="http:/ ...
- 【活动】美团技术沙龙第49期:AI在外卖场景中的最佳实践
美团技术沙龙第49期开始啦! 本次沙龙,美团外卖技术部专家会深入介绍AI在对话系统.图像处理.个性化推荐.智能营销等方向在外卖业务中的实践,希望与业界技术同学一起交流学习. 无论你从事智能搜索,或是算 ...
- python网络编程socket编程(TCP、UDP客户端服务器)
摘录 python核心编程 使用socket()模块函数创建套接字——通信端点 >>> from socket import * >>> tcpSock = soc ...
- python函数编程-偏函数partial function
一般的,通过设定函数参数的默认值,可以减低函数调用的难度.比如:int()函数可以把字符串转换成整数: >>> int(') 123 事实上,int()函数还提供额外的base参数, ...
- grid行高亮显示
grid var grid = new Ext.RSEGrid({ //数据显示面板 id: 'O005_grid', title: '图纸目录', loadMask: true, autoScrol ...
- DC8: Vulnhub Walkthrough
镜像下载链接: https://www.vulnhub.com/entry/dc-8,367/#download 主机扫描: http://10.10.202.131/?nid=2%27 http:/ ...
- Neo4j安装过程详解
在安装neo4j之前,需要安装Java JRE,并配置Java开发环境,然后安装neo4j服务. 一.CentOS下安装 1.下载Neo4j 去官网下载最新的neo4j,选择社区版.地址:https: ...
- Android Healthd电池服务分析
healthd healthd是安卓4.4之后提出来的,监听来自kernel的电池事件,并向上传递电池数据给framework层的BatteryService.BatteryService计算电池电量 ...
- H5 video poster属性—设置视频封面
打开一个视频,在点击播放之前会看到一张封面图,点击之后封面消失,随即播放视频. 若现有一需求,要你自定义给某个视频设置封面,应该怎么做呢? 此时可使用H5 video提供的poster属性即 ...