/// <summary> /// 获取此实例所表示的日期所在年的周数 /// </summary> /// <param name="this">给定的日期(年月日)</param> /// <returns>所在季度的周数</returns> public static int WeeksOfYear(this in DateOnly @this) { var end = new DateOnly(@this.Year, 12, 31); var gc = new System.Globalization.DateTimeFormatInfo().Calendar; return(gc.GetWeekOfYear(end.ToDateTime(TimeOnly.MinValue), System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday)); }
/// <summary> /// Given a Date, Converts it into a double representing its internal Excel representation, /// which Is the number of days since 1/1/1900. Fractional days represent hours, minutes, and seconds. /// </summary> /// <param name="date">The date.</param> /// <param name="use1904windowing">Should 1900 or 1904 date windowing be used?</param> /// <returns>Excel representation of Date (-1 if error - test for error by Checking for less than 0.1)</returns> public static double GetExcelDate(DateOnly date, bool use1904windowing) { if ((!use1904windowing && date.Year < 1900) || //1900 date system must bigger than 1900 (use1904windowing && date.Year < 1904)) //1904 date system must bigger than 1904 { return(BAD_DATE); } DateOnly startdate; if (use1904windowing) { startdate = new DateOnly(1904, 1, 1); } else { startdate = new DateOnly(1900, 1, 1); } double value = (date.ToDateTime(TimeOnly.MinValue) - startdate.ToDateTime(TimeOnly.MinValue)).TotalDays + 1; if (!use1904windowing && value >= 60) { value++; } else if (use1904windowing) { value--; } return(value); }
public void ToDateTime_Unspecified() { DateOnly date = new DateOnly(2000, 1, 1); DateTime expectedDate = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); DateTime dateTime = date.ToDateTime(); Assert.Equal(expectedDate, dateTime); }
public void ToDateTime_Utc() { DateOnly date = new DateOnly(2000, 1, 1); DateTime expectedDate = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc); DateTime dateTime1 = date.ToUtcDateTime(); DateTime dateTime2 = date.ToDateTime(DateTimeKind.Utc); Assert.Equal(expectedDate, dateTime1); Assert.Equal(expectedDate, dateTime2); }
public static void DateTimeConversionTest() { DateTime dt = DateTime.Today; DateOnly dateOnly = DateOnly.FromDateTime(dt); Assert.Equal(dt.Year, dateOnly.Year); Assert.Equal(dt.Month, dateOnly.Month); Assert.Equal(dt.Day, dateOnly.Day); dt = dateOnly.ToDateTime(new TimeOnly(1, 10, 20)); Assert.Equal(dateOnly.Year, dt.Year); Assert.Equal(dateOnly.Month, dt.Month); Assert.Equal(dateOnly.Day, dt.Day); Assert.Equal(1, dt.Hour); Assert.Equal(10, dt.Minute); Assert.Equal(20, dt.Second); Assert.Equal(DateTimeKind.Unspecified, dt.Kind); dt = dateOnly.ToDateTime(new TimeOnly(23, 59, 59), DateTimeKind.Utc); Assert.Equal(dateOnly.Year, dt.Year); Assert.Equal(dateOnly.Month, dt.Month); Assert.Equal(dateOnly.Day, dt.Day); Assert.Equal(23, dt.Hour); Assert.Equal(59, dt.Minute); Assert.Equal(59, dt.Second); Assert.Equal(DateTimeKind.Utc, dt.Kind); dt = dateOnly.ToDateTime(new TimeOnly(23, 59, 59), DateTimeKind.Local); Assert.Equal(DateTimeKind.Local, dt.Kind); dateOnly = DateOnly.FromDateTime(dt); Assert.Equal(dt.Year, dateOnly.Year); Assert.Equal(dt.Month, dateOnly.Month); Assert.Equal(dt.Day, dateOnly.Day); }
// This will look for payments between the two dates but also transfers up to 5 days before the toDate and // 5 days before the fromDate private async Task <List <VenmoTransaction> > GetVenmoTransactionsBetweenDates(DateOnly fromDate, DateOnly toDate, VenmoApi venmoApi, Database.Models.VenmoUser venmoUser) { logger.LogDebug($"Finding Venmo transactions between {fromDate} and {toDate}"); List <VenmoTransaction> transactions = new List <VenmoTransaction>(); string? beforeId = null; bool foundAllTransactions = false; VenmoTransaction?toTransfer = null; VenmoTransaction?fromTransfer = null; VenmoTransaction?previousTransaction = null; do { var transactionsResponse = await venmoApi.GetTransactions(beforeId); if (transactionsResponse.Data != null) { foreach (var transaction in transactionsResponse.Data) { if (transaction.DateCreated != null && transaction.Type != null) { if (transaction.Type == "transfer") { DateTime transactionDate = DateTime.Parse(transaction.DateCreated); logger.LogDebug($"Found transfer on {transactionDate}. ID: {transaction.Id}"); // if the transfer initiated from Venmo was before the toDate and after 5 days before // the toDate set the transaction as the toTransfer if (toTransfer == null && transactionDate < toDate.ToDateTime(TimeOnly.MinValue) && transactionDate > toDate.AddDays(-5).ToDateTime(TimeOnly.MinValue)) { logger.LogDebug($"Set transfer {transaction.Id} as toTransfer"); toTransfer = transaction; transactions.Add(transaction); } // need to handle transfers done one after another (due to Venmo transfer limit) if (toTransfer != null && previousTransaction != null && previousTransaction.Type == "transfer") { logger.LogDebug($"Found transfers one after another\n" + $"Transfer 1: {previousTransaction.Id} at {previousTransaction.DateCreated}\n" + $"Transfer 2: {transaction.Id} at {transaction.DateCreated}"); transactions.Add(transaction); } if (fromTransfer == null && transactionDate < fromDate.ToDateTime(TimeOnly.MinValue) && transactionDate > fromDate.AddDays(-5).ToDateTime(TimeOnly.MinValue)) { logger.LogDebug($"Set transfer {transaction.Id} as fromTransfer"); fromTransfer = transaction; transactions.Add(transaction); foundAllTransactions = true; break; } } else if (transaction.Type == "payment") { // if the user received money, either someone paid them or they charged someone if ((transaction.Payment !.Action == "pay" && transaction.Payment.Target !.User.Id == venmoUser.Venmo !.UserId) || (transaction.Payment.Action == "charge" && transaction.Payment.Actor !.Id == venmoUser.Venmo !.UserId)) { DateTime transactionDate = DateTime.Parse(transaction.DateCreated); if (toTransfer != null) { if (fromTransfer == null) { if (transactionDate < DateTime.Parse(toTransfer.DateCreated !)) { transactions.Add(transaction); } } } } } previousTransaction = transaction; } } if (transactionsResponse.Data.Count == 0) { break; } beforeId = previousTransaction !.Id; } }while (!foundAllTransactions); return(transactions); }
public DateTime GetPreviousWorkDate(DateOnly day) { day = GetPreviousWorkDay(day); return(day.ToDateTime(Midnight, DateTimeKind.Local).ToUniversalTime()); }
/// <summary> /// Converts a <see cref="DateOnly"/> to a Local <see cref="DateTime"/>. /// </summary> /// <param name="date">The <see cref="DateOnly"/> to convert.</param> /// <returns>A local <see cref="DateTime"/>.</returns> public static DateTime ToLocalDateTime(this DateOnly date) => date.ToDateTime(DateTimeKind.Local);
/// <summary> /// Converts a <see cref="DateOnly"/> to a UTC <see cref="DateTime"/>. /// </summary> /// <param name="date">The <see cref="DateOnly"/> to convert.</param> /// <returns>A UTC <see cref="DateTime"/>.</returns> public static DateTime ToUtcDateTime(this DateOnly date) => date.ToDateTime(DateTimeKind.Utc);
///<summary>Uses Zero-timespan and unspecified datetime-kind</summary> public static DateTime ToDateTime(this DateOnly thisDate) { TimeOnly time = TimeOnly.FromTimeSpan(TimeSpan.Zero); return(thisDate.ToDateTime(time, DateTimeKind.Unspecified)); }
public static DateTime ToDateTime(this DateOnly date, DateTimeKind kind) { return(date.ToDateTime(new TimeOnly(), kind)); }
public static DateTime ToDateTime(this DateOnly date) { return(date.ToDateTime(new TimeOnly())); }