(エッセンス)2020年6月26日C#クラスライブラリDateTime(拡張方法)
13152 ワード
using NodaTime;
using System;
using System.Globalization;
namespace Core.Util
{
public static partial class Extention
{
///
///
///
///
///
public static int GetWeekOfYear(this DateTime dateTime)
{
GregorianCalendar gc = new GregorianCalendar();
return gc.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
}
///
/// Js timestamp
///
///
///
public static long ToJsTimestamp(this DateTime dateTime)
{
var startTime = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime();
long result = (dateTime.Ticks - startTime.Ticks) / 10000; // 10000 13
return result;
}
///
/// js getTime()
///
///
///
public static Int64 JsGetTime(this DateTime dt)
{
Int64 retval = 0;
var st = new DateTime(1970, 1, 1);
TimeSpan t = (dt.ToUniversalTime() - st);
retval = (Int64)(t.TotalMilliseconds + 0.5);
return retval;
}
///
/// 1970-01-01
///
///
///
public static DateTime Default(this DateTime dt)
{
return DateTime.Parse("1970-01-01");
}
///
/// ( , Linux )
///
///
///
public static DateTime ToCstTime(this DateTime dt)
{
Instant now = SystemClock.Instance.GetCurrentInstant();
var shanghaiZone = DateTimeZoneProviders.Tzdb["Asia/Shanghai"];
return now.InZone(shanghaiZone).ToDateTimeUnspecified();
}
///
///
///
///
///
public static DateTime ToLocalTime(this DateTime time)
{
return TimeZoneInfo.ConvertTime(time, TimeZoneInfo.Local);
}
///
/// Unix ( )
///
///
///
public static int ToUnixTimeStamp(this DateTime time)
{
DateTime startTime = new DateTime(1970, 1, 1).ToLocalTime();
return (int)(time - startTime).TotalSeconds;
}
}
}