每天感觉都有事件做,但到了晚上想想今天做了什么事,又好像没有学到什么东西。。。。
所以呢,开始把每天学到的东西,写在自己的博客上,算是给自己安慰一下下。^_^
输出自定义DateTime格式和实现秒数和日期的转化:
具体可以在MSDN上用关键字“格式化字符串”搜索到内容,具体就不再重复了。写一个例子:
class Program

...{
static void Main(string[] args)

...{

byte[] Temp=new byte[]...{99,09,09,09,09,88,99,99};

byte[] Temp1 = new byte[] ...{ 99, 09, 09, 0x88, 09, 88, 99, 99 };
string str1 = GetDateTime(Temp );
string str2 = GetDateTime(Temp1 );
Console.WriteLine(str1);
Console.WriteLine(str2 );
Console.ReadLine();
}
private static string GetDateTime(byte[] Data)

...{
long milSecond=0;
long Second=0;
double totalSecond=0;
DateTime dt = new DateTime(1700, 1, 1, 0, 0, 0);
DateTime dtResult;
for (int i = 3; i >= 0; i--)

...{
milSecond <<= 8;
milSecond = milSecond ^ Data[i + 4];
}

if (milSecond > 1000)
milSecond = 0;

for (int i = 3; i >= 0; i--)

...{
Second <<= 8;
Second=Second ^Data[i];
}
totalSecond = (double )Second + (double )milSecond / 1000;
dtResult = dt.AddSeconds(totalSecond );
return dtResult.ToString("yyyy年MM月dd日HH:mm:ss");
}
}
这个例子是实现,输入一个字节数组,数组的前四位是代表秒数,后四位代表毫秒数,低字节在前,高字节在后,数组存储值表示为距1970年1月1日0时0分0秒的总秒数。这个函数是实现把这个数组转化为对应的日期,日期格式为:yyyy年MM月dd日HH:mm:ss。
例子输出如下:
1704年10月21日11:35:51
1772年04月28日10:26:43