C# 秒数转日期_由秒数得到日期几天几小时_当前日期时间,转换为秒

时间:2020-12-21 19:26:34
///<summary>
///由秒数得到日期几天几小时。。。
///</summary
///<param name="t">秒数</param>
///<param name="type">0:转换后带秒,1:转换后不带秒</param>
///<returns>几天几小时几分几秒</returns>
public static string parseTimeSeconds(int t, int type)
{
string r = "";
int day, hour, minute, second;
if (t >= ) //天,
{
day = Convert.ToInt16(t / );
hour = Convert.ToInt16((t % ) / );
minute = Convert.ToInt16((t % % ) / );
second = Convert.ToInt16(t % % % );
if (type == )
r = day + ("day") + hour + ("hour") + minute + ("minute") + second + ("second");
else
r = day + ("day") + hour + ("hour") + minute + ("minute"); }
else if (t >= )//时,
{
hour = Convert.ToInt16(t / );
minute = Convert.ToInt16((t % ) / );
second = Convert.ToInt16(t % % );
if (type == )
r = hour + ("hour") + minute + ("minute") + second + ("second");
else
r = hour + ("hour") + minute + ("minute");
}
else if (t >= )//分
{
minute = Convert.ToInt16(t / );
second = Convert.ToInt16(t % );
r = minute + ("minute") + second + ("second");
}
else
{
second = Convert.ToInt16(t);
r = second + ("second");
}
return r;
} /// <summary>
/// 当前日期时间,转换为秒
/// </summary>
/// <returns>秒数</returns>
public static string xDateSeconds()
{
long xdateseconds = ;
DateTime xdatenow = DateTime.UtcNow; //当前UTC时间 long xminute = ; //一分种60秒
long xhour = ;
long xday = ;
long byear = ;//从1970-1-1 0:00:00开始到现在所过的秒
long[] xmonth = { , , , , , , , , , , , };
long[] xyear = { , };
long num = ; xdateseconds += xdatenow.Second; //算秒
xdateseconds += xdatenow.Minute * xminute; //算分
xdateseconds += xdatenow.Hour * xhour; //算时
xdateseconds += (xdatenow.Day - ) * xday; //算天 //算月(月换成天算)
if (DateTime.IsLeapYear(xdatenow.Year))
{
xdateseconds += (xmonth[xdatenow.Month - ] + ) * xday;
}
else
{
xdateseconds += (xmonth[xdatenow.Month - ]) * xday;
} //算年(年换成天算)
long lyear = xdatenow.Year - byear; for (int i = ; i < lyear; i++)
{
if (DateTime.IsLeapYear((int)byear + i))
{
num++;
}
} xdateseconds += ((lyear - num) * xyear[] + num * xyear[]) * xday; return xdateseconds.ToString();
} /// <summary>
/// 秒数转日期
/// </summary>
/// <param name="Value">秒数</param>
/// <returns>日期</returns>
public static DateTime GetGMTDateTime(int Value)
{
//秒数转时间日期
//GMT时间从2000年1月1日开始,先把它作为赋为初值
long Year = , Month = , Day = ;
long Hour = , Min = , Sec = ;
//临时变量
long iYear = , iDay = ;
long iHour = , iMin = , iSec = ;
//计算文件创建的年份
iYear = Value / ( * * * );
Year = Year + iYear;
//计算文件除创建整年份以外还有多少天
iDay = (Value % ( * * * )) / ( * * );
//把闰年的年份数计算出来
int RInt = ;
for (int i = ; i < Year; i++)
{
if ((i % ) == )
RInt = RInt + ;
}
//计算文件创建的时间(几时)
iHour = ((Value % ( * * * )) % ( * * )) / ( * );
Hour = Hour + iHour;
//计算文件创建的时间(几分)
iMin = (((Value % ( * * * )) % ( * * )) % ( * )) / ;
Min = Min + iMin;
//计算文件创建的时间(几秒)
iSec = (((Value % ( * * * )) % ( * * )) % ( * )) % ;
Sec = Sec + iSec;
DateTime t = new DateTime((int)Year, (int)Month, (int)Day, (int)Hour, (int)Min, (int)Sec);
DateTime t1 = t.AddDays(iDay - RInt);
return t1;
} 把总秒数转换成DataTime格式,注意时区,默认是格林威治时间GMT DateTime dt = new DateTime(, , , , , , ).ToLocalTime().AddSeconds(info.lTime);
uploadtime.Text = string.Format("{0:F}", dt);