手机
当前位置:查字典教程网 >编程开发 >C#教程 >时间戳与时间相互转换(php .net精确到毫秒)
时间戳与时间相互转换(php .net精确到毫秒)
摘要:/**获取当前时间戳,精确到毫秒*/functionmicrotime_float(){list($usec,$sec)=explode("...

/** 获取当前时间戳,精确到毫秒 */ function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } /** 格式化时间戳,精确到毫秒,x代表毫秒 */ function microtime_format($tag, $time) { list($usec, $sec) = explode(".", $time); $date = date($tag,$usec); return str_replace('x', $sec, $date); }

使用方法:

1. 获取当前时间戳(精确到毫秒):microtime_float()

2. 时间戳转换时间:microtime_format('Y年m月d日 H时i分s秒 x毫秒', 1270626578

.net 时间戳互相转换(精确到毫秒)

这里记录一个时间戳的互相转换方法,网上都找了,基本都没有精确到毫秒,我的这个基本可以满足精确到毫秒的级别,代码如下:

/// <summary> /// Unix时间戳转换为DateTime /// </summary> private DateTime ConvertToDateTime(string timestamp) { System.DateTime time = System.DateTime.MinValue; //精确到毫秒 //时间戳转成时间 DateTime start = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(, , )); try { time = timestamp.Length == ? start.AddSeconds(long.Parse(timestamp)) : start.AddMilliseconds(long.Parse(timestamp)); } catch (Exception ex) { return start;//转换失败 } return time; } /// <summary> /// DateTime转换为Unix时间戳 /// </summary> /// <param name="time"></param> /// <returns></returns> private string ConvertTimestamp(DateTime time) { double intResult = ; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(, , )); intResult = (time - startTime).TotalMilliseconds; return Math.Round(intResult,).ToString(); }

【时间戳与时间相互转换(php .net精确到毫秒)】相关文章:

c# 正则指引--字符组

深入Unix时间戳与C# DateTime时间类型互换的详解

C# DES加密算法中向量的作用详细解析

c# 曲线图生成代码

客户端实现蓝牙接收(C#)知识总结

C#中时间的几种格式转换方法

比较有效的使用C#读取文件的代码

C# 委托(跨窗体操作控件)实例流程讲解

c# 服务器上传木马监控代码(包含可疑文件)

C# byte数组与Image相互转换的方法

精品推荐
分类导航