This commit is contained in:
Bright 2025-07-15 15:54:50 +07:00
parent 86e594df09
commit a352ec6578
2 changed files with 79 additions and 3 deletions

View file

@ -12,6 +12,7 @@ using BMA.EHR.Recurit.Service.Data;
using System.Security.Claims;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using System.Globalization;
namespace BMA.EHR.Recruit.Service.Services
{
@ -388,6 +389,79 @@ namespace BMA.EHR.Recruit.Service.Services
throw;
}
}
public DateTime CheckDateTime(string Date, string Formate)
{
// ตอนนี้ทำไว้ให้รองรับแค่ "dd/MM/yyyy", "yyyy-MM-dd"
if (string.IsNullOrWhiteSpace(Date))
return DateTime.MinValue;
string[] parts = Date.Trim().Replace("-", "/").Split("/");
if (parts.Length != 3)
return DateTime.MinValue;
int year;
int month;
int day;
switch (Formate)
{
case "dd/MM/yyyy":
if (int.TryParse(parts[2], out year) && year > 2500)
{
year -= 543;
}
else if (!int.TryParse(parts[2], out year))
{
return DateTime.MinValue;
}
if (!int.TryParse(parts[1], out month))
return DateTime.MinValue;
if (!int.TryParse(parts[0], out day))
return DateTime.MinValue;
break;
case "yyyy-MM-dd":
if (int.TryParse(parts[0], out year) && year > 2500)
{
year -= 543;
}
else if (!int.TryParse(parts[0], out year))
{
return DateTime.MinValue;
}
if (!int.TryParse(parts[1], out month))
return DateTime.MinValue;
if (!int.TryParse(parts[2], out day))
return DateTime.MinValue;
break;
default:
return DateTime.MinValue;
}
if (month < 1 || month > 12)
month = 1;
int maxDay = DateTime.DaysInMonth(year, month);
if (day < 1)
day = 1;
else if (day > maxDay)
day = maxDay;
var normalDate = $"{day}/{(month >= 1 && month <= 9 ? $"0{month}" : month)}/{year}";
if (DateTime.TryParseExact(normalDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime parsedDate))
{
return parsedDate;
}
return DateTime.MinValue;
}
}
}