C#中unix与windows时间格式互转
发表于:2024-10-06 作者:千家信息网编辑
千家信息网最后更新 2024年10月06日,最近做shopEX平台订单对接,因为shopEX是Php的,所以在提交订单的时间格式首先转换成unix时间格式,然后才能提交成功。相同的,有response返回的所有的日期格式都是unix时间格式,所
千家信息网最后更新 2024年10月06日C#中unix与windows时间格式互转最近做shopEX平台订单对接,因为shopEX是Php的,所以在提交订单的时间格式首先转换成unix时间格式,然后才能提交成功。
相同的,有response返回的所有的日期格式都是unix时间格式,所以要转换成系统时间我们才能使用。
在网上搜了一下,整理了一下,下面是我用C#编写的小例子,供各位参考。数据的严谨性,需要在实际使用时进行相应的判断操作。
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
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; }
}
}
相同的,有response返回的所有的日期格式都是unix时间格式,所以要转换成系统时间我们才能使用。
在网上搜了一下,整理了一下,下面是我用C#编写的小例子,供各位参考。数据的严谨性,需要在实际使用时进行相应的判断操作。
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
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
///
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; }
}
}
格式
时间
日期
订单
C#
严谨
相同
成功
严谨性
例子
前提
字符
字符串
实际
平台
数据
系统
参考
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
家游网络技术有限公司
河北省计算机网络技术专接本
小学生网络安全儿歌图片
怎么炸网易服务器
sql数据库删除冗余数据
网络安全监控转发视频原理
成都云南网络技术服务
淘宝店铺出售带服务器
如何使用语句在数据库中
数据库架构分离
余姚一站式软件开发企业
中国最大的软件开发公司排名
六盘水 服务器
三计算机三级网络技术
深圳格物米网络技术
编译原理 数据库原理
设备软件开发费用定义
攻击服务器是啥意思
全民奇迹服务器数据库名字
csgo服务器一直在下载
我的世界十大强势服务器
页游服务器架设教程
软件开发模型生命周期模型
网络安全大练兵
搜小说网服务器出错
localdb数据库备份
加密服务器锁了电脑系统
网络技术服务公司营业税率
网络安全尹鹏
怎样破解数据库