48 lines
1.6 KiB
C#
48 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Church.Net.Utility
|
|
{
|
|
public static class DateTimeHelper
|
|
{
|
|
|
|
private static TimeZoneInfo pacificZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
|
|
public static DateTime GetNextWeekday(DateTime start, DayOfWeek day)
|
|
{
|
|
// The (... + 7) % 7 ensures we end up with a value in the range [0, 6]
|
|
int daysToAdd = ((int)day - (int)start.DayOfWeek + 7) % 7;
|
|
return start.AddDays(daysToAdd);
|
|
}
|
|
public static DateTime Now(string timeZoneCode=null)
|
|
{
|
|
TimeZoneInfo timeZone;
|
|
if (null== timeZoneCode)
|
|
{
|
|
timeZone = pacificZone;
|
|
}
|
|
else
|
|
{
|
|
timeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneCode);
|
|
}
|
|
return TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, pacificZone);
|
|
}
|
|
public static DateTime Today(string timeZoneCode = null)
|
|
{
|
|
return Now(timeZoneCode).Date;
|
|
}
|
|
public static DateTime ToUtc(this DateTime time,string timeZoneCode)
|
|
{
|
|
TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneCode);
|
|
return TimeZoneInfo.ConvertTimeToUtc(time, timeZone);
|
|
}
|
|
public static DateTime ToLocal(this DateTime time, string timeZoneCode)
|
|
{
|
|
TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneCode);
|
|
return TimeZoneInfo.ConvertTimeFromUtc(time, timeZone);
|
|
}
|
|
}
|
|
}
|