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: ...
随机推荐
- C# yield关键字
关于yield关键字,网上有很多文章介绍了,但是看了之后,虽然明白了"哦,原来是这么回事",但是在日常开发中并没有真正的用起来,所以,写此一篇,介绍一下在真正的项目中怎么使用这个关 ...
- java对象的实例化过程
简单类对象的实例化过程 1.在方法区加载类: 2.在栈内存申请空间,声明变量P: 3.在堆内存中开辟空间,分配对象地址: 4.在对象空间中,对对象的属性进行默认初始化,类成员变量显示初始化: 5.构造 ...
- 新人踩坑的一天——springboot注入mapper时出现java.lang.NullPointerException: null
来公司的第二周接到了定时任务的开发需求:每天早上十点发送用户报表邮件 .校招新人菜鸟没做过这玩意有些懵(尴尬)于是决定分步写,从excel导出->邮件发送->定时器实现->mappe ...
- JS---DOM---总结绑定事件的区别
总结绑定事件的区别: addEventListener(); attachEvent() 相同点: 都可以为元素绑定事件 不同点: 1.方法名不一样 2.参数个数不一样addEventListen ...
- TP5 try{}catch{}异常捕获不到 解决办法
问题:TP5用下面的方法, 想要获取抛出的异常捕获不到…… 错误的写法: try{ …… } catch (Exception $e) { echo $e->getMessage(); } 正确 ...
- 主说明:自动Undo管理的故障排除指南(Doc ID 1579081.1)
Master Note: Troubleshooting guide for Automatic Undo Management (Doc ID 1579081.1) APPLIES TO: Orac ...
- Vim 基本的使用
三种模式 按 ESC 进入命令模式 命令模式下输入 Shift + : 进入末行模式 命令模式下输入插入命令,如(i,a,o) 进入输入模式 进入 vim 文件名 vim直接编辑一个文件,如果是已经存 ...
- 集合系列 Map(十四):WeakedHashMap
WeakedHashMap 也是 Map 集合的哈希实现,但其余 HashMap 的不同之处在于.其每个节点的 value 引用是弱引用,可以方便 GC 回收. public class WeakHa ...
- C# TreeView模糊查找节点
List<TreeNode> listSearchTreeNodes; private void btn_Search_Click(object sender, EventArgs e) ...
- go笔记--json包使用
目录 Marshal Unmarshal 处理json对象 @ json包实现了json对象的编解码,参见RFC 4627.Json对象和go类型的映射关系主要通过Marshal和Unmarshal函 ...