using System; using System.Linq; public class DateTimeHelper { public static string GetDigits(string s) => new string(s?.Where(char.IsDigit).ToArray()); public static DateTime? ToDateTimeFromInts(int? dateInt, int? timeInt) { var date = GetDateFromInt(dateInt); if (date == null) return null; var time = GetTimeFromInt(timeInt); if (time != null) date = date.Value + time.Value; return date; } private static TimeSpan? GetTimeFromInt(int? timeInt) { var timeStr = GetDigits(timeInt?.ToString() ?? ""); if (timeStr.Length < 4) return null; var hour = int.Parse(timeStr.Substring(0, 2)); var minute = int.Parse(timeStr.Substring(2, 2)); var second = 0; if (timeStr.Length >= 6) second = int.Parse(timeStr.Substring(4, 2)); return new TimeSpan(hour, minute, second); } private static DateTime? GetDateFromInt(int? dateInt) { var dateStr = GetDigits(dateInt?.ToString() ?? ""); if (dateStr.Length < 8) return null; var year = int.Parse(dateStr.Substring(0, 4)); var month = int.Parse(dateStr.Substring(4, 2)); var date = int.Parse(dateStr.Substring(6, 2)); var dateTime = new DateTime(year, month, date); return dateTime; } }
418200cookie-checkC# int(s) to datetime