本文整理汇总了C#中DateTime.AddSeconds方法的典型用法代码示例。如果您正苦于以下问题:C# DateTime.AddSeconds方法的具体用法?C# DateTime.AddSeconds怎么用?C# DateTime.AddSeconds使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DateTime
的用法示例。
在下文中一共展示了DateTime.AddSeconds方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UnixTimeStampToDateTime
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
{
// Unix timestamp is seconds past epoch
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp);
return dtDateTime;
}
示例2: InstantToDateTime
public static DateTime InstantToDateTime(double instant, DateTime start, TimeUnits units = TimeUnits.SECONDS)
{
DateTime instantAsDate = start;
switch (units)
{
case TimeUnits.YEARS:
instantAsDate = start.AddYears((int)instant);
break;
case TimeUnits.MONTHS:
instantAsDate = start.AddMonths((int)instant);
break;
case TimeUnits.DAYS:
instantAsDate = start.AddDays(instant);
break;
case TimeUnits.HOURS:
instantAsDate = start.AddHours(instant);
break;
case TimeUnits.MINUTES:
instantAsDate = start.AddMinutes(instant);
break;
case TimeUnits.SECONDS:
instantAsDate = start.AddSeconds(instant);
break;
}
return instantAsDate;
}
示例3: GetBuildDateTime
/// <summary>
/// 在C#中指定修订号为*的时候,修订号则是一个时间戳,我们可以从其得到编译日期
/// 生成和修订版本号必须是自动生成的
/// AssemblyInfo里的写法应该为1.0之后为.*
/// [assembly: AssemblyVersion("1.0.*")]
/// </summary>
/// <returns></returns>
public DateTime GetBuildDateTime()
{
/*
* version = 1.0.3420.56234
* 这里主版本号是1,次版本号是0,而生成和修订版本号是自动生成的。
* 使用*时,生成号是从2000年1月1日开始的天数,而修订号是从凌晨开始的秒数除以2。
*/
string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
int days = 0;
int seconds = 0;
string[] v = version.Split('.');
if (v.Length == 4)
{
days = Convert.ToInt32(v[2]);
seconds = Convert.ToInt32(v[3]);
}
DateTime dt = new DateTime(2000, 1, 1);
dt = dt.AddDays(days);
dt = dt.AddSeconds(seconds * 2);
return dt;
}
示例4: GetLinkerTimestamp
public static DateTime GetLinkerTimestamp(Assembly assembly = null)
{
var asm = assembly ?? Assembly.GetCallingAssembly();
var filePath = asm.Location;
const int peHeaderOffset = 60;
const int linkerTimestampOffset = 8;
var b = new byte[2048];
Stream s = null;
try
{
s = new FileStream(filePath, FileMode.Open, FileAccess.Read);
s.Read(b, 0, 2048);
}
finally
{
s?.Close();
}
var i = BitConverter.ToInt32(b, peHeaderOffset);
var secondsSince1970 = BitConverter.ToInt32(b, i + linkerTimestampOffset);
var dt = new DateTime(1970, 1, 1, 0, 0, 0);
return dt.AddSeconds(secondsSince1970);
}
示例5: CanAddSecondsAcrossDstTransition
public void CanAddSecondsAcrossDstTransition()
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var dt = new DateTime(2015, 3, 8, 1, 59, 59);
var result = dt.AddSeconds(1, tz);
var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7));
Assert.Equal(expected, result);
Assert.Equal(expected.Offset, result.Offset);
}
示例6: BuiltTime
private static DateTime BuiltTime()
{
string filePath = System.Reflection.Assembly.GetCallingAssembly().Location;
const int c_PeHeaderOffset = 60;
const int c_LinkerTimestampOffset = 8;
byte[] b = new byte[2048];
System.IO.Stream s = null;
try
{
s = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
s.Read(b, 0, 2048);
}
finally
{
if (s != null)
{
s.Close();
}
}
int i = System.BitConverter.ToInt32(b, c_PeHeaderOffset);
int secondsSince1970 = System.BitConverter.ToInt32(b, i + c_LinkerTimestampOffset);
DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
dt = dt.AddSeconds(secondsSince1970);
dt = dt.ToLocalTime();
return dt;
}
示例7: FromDateTime
public static TdsDateTime FromDateTime(DateTime dateTime, byte cb)
{
SqlDateTime sqlDateTime;
TdsDateTime tdsDateTime = new TdsDateTime();
Debug.Assert(cb == 8 || cb == 4, "Invalid date time size!");
if (cb == 8)
{
sqlDateTime = new SqlDateTime(dateTime);
tdsDateTime.time = sqlDateTime.TimeTicks;
}
else
{
// note that smalldatetime is days&minutes.
// Adding 30 seconds ensures proper roundup if the seconds are >= 30
// The AddSeconds function handles eventual carryover
sqlDateTime = new SqlDateTime(dateTime.AddSeconds(30));
tdsDateTime.time = sqlDateTime.TimeTicks / SqlDateTime.SQLTicksPerMinute;
}
tdsDateTime.days = sqlDateTime.DayTicks;
return tdsDateTime;
}
示例8: DateFromUnixTime
public static DateTime DateFromUnixTime(this long unixTime)
{
var dt = new DateTime(1970, 1, 1, 1, 0, 0, 0, 0);
return dt.AddSeconds(unixTime).ToLocalTime();
}