first commit
This commit is contained in:
295
Complex.Application/Other/Utility/PersianDate.cs
Normal file
295
Complex.Application/Other/Utility/PersianDate.cs
Normal file
@@ -0,0 +1,295 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Complex.Application.Utility
|
||||
{
|
||||
public class PersianDate
|
||||
{
|
||||
public struct PDate
|
||||
{
|
||||
public int Year { set; get; }
|
||||
public int Month { set; get; }
|
||||
public int Day { set; get; }
|
||||
}
|
||||
public PersianDate()
|
||||
{
|
||||
|
||||
}
|
||||
public static DateTime CurrentDate
|
||||
{
|
||||
get
|
||||
{
|
||||
return DateTime.Now;
|
||||
}
|
||||
}//= DateTime.Now;
|
||||
public static PersianCalendar persianDate = new PersianCalendar();
|
||||
|
||||
public static DateTime ConvertToMiladi(string PersianDateString)
|
||||
{
|
||||
if (PersianDateString.IsNullOrEmpty())
|
||||
return new DateTime();
|
||||
DateTime date;
|
||||
var splitP = PersianDateString.Substring(0, 10).Split("- /".ToCharArray()).Select(d => d.ToInt().Value).ToArray();
|
||||
date = new DateTime(splitP[0], splitP[1], splitP[2], persianDate);
|
||||
return date;
|
||||
}
|
||||
|
||||
public static int CurrentPersianYear
|
||||
{
|
||||
get
|
||||
{
|
||||
return persianDate.GetYear(CurrentDate);
|
||||
}
|
||||
}
|
||||
public static int CurrentPersianMonth
|
||||
{
|
||||
get
|
||||
{
|
||||
return persianDate.GetMonth(CurrentDate);
|
||||
}
|
||||
}
|
||||
public static int CurrentPersianDay
|
||||
{
|
||||
get
|
||||
{
|
||||
return persianDate.GetDayOfMonth(CurrentDate);
|
||||
}
|
||||
}
|
||||
public static List<string> PersianWeekDay = new List<string>()
|
||||
{
|
||||
"شنبه",
|
||||
"یکشنبه",
|
||||
"دوشنبه",
|
||||
"سه شنبه",
|
||||
"چهارشنبه",
|
||||
"پنج شنبه",
|
||||
"جمعه"
|
||||
};
|
||||
public static List<string> PersianMonth = new List<string>()
|
||||
{
|
||||
"فروردین",
|
||||
"اردیبهشت",
|
||||
"خرداد",
|
||||
"تیر",
|
||||
"مرداد",
|
||||
"شهریور",
|
||||
"مهر",
|
||||
"آبان",
|
||||
"آذر",
|
||||
"دی",
|
||||
"بهمن",
|
||||
"اسفند"
|
||||
};
|
||||
public static string CurrentMonth
|
||||
{
|
||||
get
|
||||
{
|
||||
return PersianMonth[persianDate.GetMonth(DateTime.Now) - 1];
|
||||
}
|
||||
}
|
||||
public static string CurrntWeekDay
|
||||
{
|
||||
get
|
||||
{
|
||||
try
|
||||
{
|
||||
var d = (int)persianDate.GetDayOfWeek(DateTime.Now);
|
||||
if (d == 6)
|
||||
d = -1;
|
||||
return PersianWeekDay[d + 1];
|
||||
}
|
||||
catch { return persianDate.GetDayOfWeek(DateTime.Now).ToString() + "," + (int)persianDate.GetDayOfWeek(DateTime.Now); }
|
||||
}
|
||||
}
|
||||
public static int CurrentDaysInMonth
|
||||
{
|
||||
get
|
||||
{
|
||||
return persianDate.GetDaysInMonth(CurrentPersianYear, CurrentPersianMonth);
|
||||
}
|
||||
}
|
||||
public static int StartDayInWeek(int Year, int Month)
|
||||
{
|
||||
DateTime dt = ConvertToMiladi(Year + "/" + Month.ToString().PadLeft(2, '0') + "/01");
|
||||
var d = (int)dt.DayOfWeek;
|
||||
if (d == 6)
|
||||
d = -1;
|
||||
return d + 1;
|
||||
}
|
||||
public static int CurrentDayInWeek
|
||||
{
|
||||
get
|
||||
{
|
||||
try
|
||||
{
|
||||
var d = (int)persianDate.GetDayOfWeek(DateTime.Now);
|
||||
if (d == 6)
|
||||
d = -1;
|
||||
return d + 1;
|
||||
}
|
||||
catch { return 0; }
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetToday()
|
||||
{
|
||||
return GetDate(DateTime.Now);
|
||||
|
||||
//return Year + "/" + ((Month < 10) ? "0" + Month : Month.ToString()) + ((Day < 10) ? "0" + Day : Day.ToString());
|
||||
}
|
||||
public static string GetDateStringFormat(string date, bool showIntervalDate = false)
|
||||
{
|
||||
if (date.IsNullOrEmpty())
|
||||
return "--";
|
||||
date = date.Trim();
|
||||
string today = GetToday();
|
||||
if (date == today)
|
||||
return "امروز";
|
||||
string yesterday = GetDate(DateTime.Now.AddDays(-1));
|
||||
if (yesterday == today)
|
||||
return "دیروز";
|
||||
string twoYesterday = GetDate(DateTime.Now.AddDays(-2));
|
||||
if (twoYesterday == today)
|
||||
return "دو روز پیش";
|
||||
string treeYesterday = GetDate(DateTime.Now.AddDays(-3));
|
||||
if (treeYesterday == today)
|
||||
return "سه روز پیش";
|
||||
if (showIntervalDate)
|
||||
{
|
||||
var m1 = ConvertToMiladi(date);
|
||||
var diff = DateTime.Now - m1;
|
||||
|
||||
return (int)diff.TotalDays + " روز پیش";
|
||||
}
|
||||
else
|
||||
return date.Substring(8).ToInt() + " " + PersianMonth[date.Substring(5, 2).ToInt().Value - 1] + " ماه " + date.Substring(0, 4);
|
||||
}
|
||||
|
||||
public static int? GetDateDiff(string date1, string date2)
|
||||
{
|
||||
if (date1.IsNullOrEmpty() || date2.IsNullOrEmpty())
|
||||
return null;
|
||||
string today = GetToday();
|
||||
var m1 = ConvertToMiladi(date1);
|
||||
var m2 = ConvertToMiladi(date2);
|
||||
var diff = m2 - m1;
|
||||
|
||||
return (int)diff.TotalDays;
|
||||
}
|
||||
|
||||
public static string GetDate(DateTime day)
|
||||
{
|
||||
int Year = persianDate.GetYear(day);
|
||||
int Month = persianDate.GetMonth(day);
|
||||
int Day = persianDate.GetDayOfMonth(day);
|
||||
|
||||
return string.Format("{0:D2}/{1:D2}/{2:D2}", Year, Month, Day);
|
||||
//return Year + "/" + ((Month < 10) ? "0" + Month : Month.ToString()) + ((Day < 10) ? "0" + Day : Day.ToString());
|
||||
}
|
||||
public static PDate GetPDate(DateTime day)
|
||||
{
|
||||
PDate pdate = new PDate();
|
||||
pdate.Year = persianDate.GetYear(day);
|
||||
pdate.Month = persianDate.GetMonth(day);
|
||||
pdate.Day = persianDate.GetDayOfMonth(day);
|
||||
return pdate;
|
||||
}
|
||||
public static string ToStandard(string _date)
|
||||
{
|
||||
if (_date.IsNullOrEmpty())
|
||||
return _date;
|
||||
try
|
||||
{
|
||||
var a = _date.Split('/').Select(i => i.ToInt().Value).ToArray();
|
||||
if (a.Count() < 2)
|
||||
return _date;
|
||||
return string.Format("{0:D2}/{1:D2}/{2:D2}", a[0], a[1], a[2]);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "";
|
||||
}
|
||||
//return Year + "/" + ((Month < 10) ? "0" + Month : Month.ToString()) + ((Day < 10) ? "0" + Day : Day.ToString());
|
||||
}
|
||||
|
||||
public static string GetNowTime(bool BySecond = false, bool ByMiliSecond = false)
|
||||
{
|
||||
DateTime dnow = DateTime.Now;
|
||||
return GetTime(dnow, BySecond, ByMiliSecond);
|
||||
}
|
||||
public static string GetTime(DateTime DT, bool BySecond = false, bool ByMiliSecond = false)
|
||||
{
|
||||
int minutes = DT.Minute;
|
||||
int Hour = DT.Hour;
|
||||
int Second = DT.Second;
|
||||
bool AN = DT.ToLongTimeString().ToLower().Contains("pm") || DT.ToLongTimeString().Contains("ب");
|
||||
|
||||
if (AN && Hour < 12)
|
||||
Hour += 12;
|
||||
|
||||
string time = string.Format("{0:D2}:{1:D2}", Hour, minutes); //((Hour < 10) ? "0" + Hour : Hour.ToString()) + ":" + ((minutes < 10) ? "0" + minutes : minutes.ToString());
|
||||
if (BySecond)
|
||||
time += ":" + (Second < 10 ? "0" + Second : Second.ToString());
|
||||
if (ByMiliSecond)
|
||||
time += ":" + DT.Millisecond;
|
||||
|
||||
return time;
|
||||
}
|
||||
|
||||
public static string GetNowDateTime(bool BySecond = false, bool ByMiliSecond = false)
|
||||
{
|
||||
return GetToday() + " - " + GetNowTime(BySecond, ByMiliSecond);
|
||||
}
|
||||
|
||||
public static string GetDateTime(DateTime DT, bool BySecond = false)
|
||||
{
|
||||
return GetDate(DT) + " - " + GetTime(DT, BySecond);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// محاسبه اختلاف دو زمان به ساعت و دقیقه
|
||||
/// </summary>
|
||||
/// <param name="beginTime">زمان شروع</param>
|
||||
/// <param name="endTime">زمان پایان</param>
|
||||
/// <returns></returns>
|
||||
public static string GetTimeDiff(string beginTime, string endTime)
|
||||
{
|
||||
var begin = beginTime.Split(':').Select(int.Parse).ToArray();
|
||||
var end = endTime.Split(':').Select(int.Parse).ToArray();
|
||||
int beginMinutes = begin[0] * 60 + begin[1];
|
||||
int endMinutes = end[0] * 60 + end[1];
|
||||
|
||||
int diffMinutes = endMinutes - beginMinutes;
|
||||
|
||||
int hour = diffMinutes / 60;
|
||||
int minute = diffMinutes % 60;
|
||||
|
||||
return string.Format("{0:D2}:{1:D2}", hour, minute);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// بررسی صحت یک رشته از نظر فرمت تاریخ
|
||||
/// </summary>
|
||||
/// <param name="date">تاریخ</param>
|
||||
/// <returns></returns>
|
||||
public static bool HasDateFormat(string date)
|
||||
{
|
||||
Regex regex = new Regex(@"^([12]\d{3}\/(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01]))$");
|
||||
return regex.IsMatch(date);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// بررسی صحت یک رشته از نظر فرمت زمان
|
||||
/// </summary>
|
||||
/// <param name="time">زمان</param>
|
||||
/// <returns></returns>
|
||||
public static bool HasTimeFormat(string time)
|
||||
{
|
||||
Regex regex = new Regex(@"^([01]?[0-9]|2[0-3]):[0-5][0-9]$");
|
||||
return regex.IsMatch(time);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user