نمایش نتایج 1 تا 9 از 9

نام تاپیک: کلاسی برای تبدیل تاریخ

Threaded View

پست قبلی پست قبلی   پست بعدی پست بعدی
  1. #1
    کاربر دائمی آواتار ROSTAM2
    تاریخ عضویت
    اسفند 1390
    محل زندگی
    فارس
    پست
    2,007

    Cool کلاسی برای تبدیل تاریخ

    سلام به همه.
    من یک کلاس ایجاد کردم به نام PersianDate که تنها با گرفتن تاریخ میلادی تاریخ رو به شمسی تبدیل می کنه.
    همچنین می تونه تاریخ شمسی خودش رو مجددا به میلادی برگردونه یعنی اگه تاریخ شمسی حتی بصورت رشته تبدیل به PersianDate بشه می شه از طریق متود ToDateTime به تاریخ میلادی تبدیل کرد و یک کلاس جامع و کامل قراره باشه برای تاریخ شمسی.
    پس اگر کلاس من در کدهاش مشکلی داشت حتما تو همین تاپیک تذکر بدید.
    PersianDate.jpg

    Imports System.Globalization
    Public Class PersianDate

    Private PCalendar As New PersianCalendar
    Public Shared Narrowing Operator CType(ByVal initialData As String) As PersianDate
    '{0}/{1}/{2} {3}:{4}:{5}
    Dim Spl As String() = initialData.Split(Space(1))
    Dim y, m, d, h, min, s As Integer
    Try
    Select Case Spl.Length
    Case 1, 2
    Dim DateSpl As String() = Spl(0).Split("/")
    y = Val(DateSpl(0))
    m = Val(DateSpl(1))
    d = Val(DateSpl(2))
    If Spl.Length = 1 Then Exit Select
    Dim TimeSpl As String() = Spl(1).Split(":")
    h = Val(TimeSpl(0))
    min = Val(TimeSpl(1))
    s = Val(TimeSpl(2))
    End Select
    Return New PersianDate(New Date(y, m, d, h, min, s, PCalendar))
    Catch ex As Exception
    Throw New FormatException(ex.Message, ex)
    End Try
    End Operator

    Public Shared Widening Operator CType(ByVal initialData As PersianDate) As String
    With initialData
    Return String.Format("{0:0000}/{1:00}/{2:00} {3:00}:{4:00}:{5:00}", .Year, .Month, .DayOfMonth, .Hour, .Minute, .Second, .MilliSecond)
    End With
    End Operator

    #Region "Properties"
    Public ReadOnly Property Eras As Integer()
    Get
    Return PCalendar.Eras
    End Get
    End Property
    Public ReadOnly Property Year() As Integer
    Get
    Return PCalendar.GetYear([Date])
    End Get
    End Property

    Public ReadOnly Property Month() As Integer
    Get
    Return PCalendar.GetMonth([Date])
    End Get
    End Property

    Public ReadOnly Property DayOfMonth() As Integer
    Get
    Return PCalendar.GetDayOfMonth([Date])
    End Get
    End Property

    Public ReadOnly Property Hour() As Integer
    Get
    Return PCalendar.GetHour([Date])
    End Get
    End Property

    Public ReadOnly Property Minute() As Integer
    Get
    Return PCalendar.GetMinute([Date])
    End Get
    End Property

    Public ReadOnly Property Second() As Integer
    Get
    Return PCalendar.GetSecond([Date])
    End Get
    End Property

    Public ReadOnly Property MilliSecond() As Integer
    Get
    Return PCalendar.GetMilliseconds([Date])
    End Get
    End Property

    Public ReadOnly Property DayOfWeek() As DayOfWeek
    Get
    Return PCalendar.GetDayOfWeek([Date])
    End Get
    End Property

    Public ReadOnly Property DayOfYear() As Integer
    Get
    Return PCalendar.GetDayOfYear([Date])
    End Get
    End Property

    Public ReadOnly Property DaysInMonth() As Integer
    Get
    Return PCalendar.GetDaysInMonth([Date].Year, [Date].Month)
    End Get
    End Property

    Public ReadOnly Property DaysInYear() As Integer
    Get
    Return PCalendar.GetDaysInYear([Date].Year)
    End Get
    End Property

    Public ReadOnly Property LeapMonth() As Integer
    Get
    Return PCalendar.GetLeapMonth([Date].Year)
    End Get
    End Property

    Public ReadOnly Property MonthsInYear() As Integer
    Get
    Return PCalendar.GetMonthsInYear([Date].Year)
    End Get
    End Property

    Public ReadOnly Property WeekOfYear() As Integer
    Get
    Return PCalendar.GetWeekOfYear([Date], CalendarWeekRule.FirstDay, DayOfWeek.Saturday)
    End Get
    End Property

    Public ReadOnly Property FourDigitYear() As Integer
    Get
    Return PCalendar.ToFourDigitYear([Date].Year)
    End Get
    End Property

    Public ReadOnly Property IsLeapYear() As Integer
    Get
    Return PCalendar.IsLeapYear([Date].Year)
    End Get
    End Property
    Public ReadOnly Property IsLeapYear(Year%) As Integer
    Get
    Return PCalendar.IsLeapYear(Year)
    End Get
    End Property
    Public ReadOnly Property IsLeapDay(Year%, Month%, Day%) As Integer
    Get
    Return PCalendar.IsLeapDay(Year%, Month%, Day%)
    End Get
    End Property
    Public ReadOnly Property IsLeapMonth(Year%, Month%, Day%) As Integer
    Get
    Return PCalendar.IsLeapMonth(Year%, Month%, Day%)
    End Get
    End Property
    #End Region

    Private [Date] As Date
    ''' <summary>
    ''' Make Persian Date form Current Gregorian Date As Now.
    ''' </summary>
    Sub New()
    MyBase.New()
    [Date] = DateAndTime.Now
    End Sub
    ''' <summary>
    ''' New Persian Date from Gregorian Date.
    ''' </summary>
    ''' <param name="GDate">Gregorian Date for convert to Persian Date.</param>
    Sub New(GDate As Date)
    MyBase.New()
    [Date] = GDate
    End Sub
    Public Overrides Function ToString() As String
    Return CType(Me, String)
    End Function
    Public Function AddHours(Value As Integer) As PersianDate
    Try
    Return New PersianDate(Me.ToDateTime.AddHours(Value))
    Catch ex As Exception
    Throw New Exception(ex.Message, ex)
    End Try
    End Function
    Public Function AddMinutes(Value As Integer) As PersianDate
    Try
    Return New PersianDate(Me.ToDateTime.AddMinutes(Value))
    Catch ex As Exception
    Throw New Exception(ex.Message, ex)
    End Try
    End Function
    Public Function AddSeconds(Value As Integer) As PersianDate
    Try
    Return New PersianDate(Me.ToDateTime.AddSeconds(Value))
    Catch ex As Exception
    Throw New Exception(ex.Message, ex)
    End Try
    End Function
    Public Function AddMilliseconds(Value As Integer) As PersianDate
    Try
    Return New PersianDate(Me.ToDateTime.AddMilliseconds(Value))
    Catch ex As Exception
    Throw New Exception(ex.Message, ex)
    End Try
    End Function
    Public Function AddTicks(Value As Integer) As PersianDate
    Try
    Return New PersianDate(Me.ToDateTime.AddTicks(Value))
    Catch ex As Exception
    Throw New Exception(ex.Message, ex)
    End Try
    End Function
    Public Function AddDays(Days As Integer) As PersianDate
    Try
    Return New PersianDate(Me.ToDateTime.AddDays(Days))
    Catch ex As Exception
    Throw New Exception(ex.Message, ex)
    End Try
    End Function
    Public Function AddMonths(months As Integer) As PersianDate
    Try
    Return New PersianDate(Me.ToDateTime.AddMonths(months))
    Catch ex As Exception
    Throw New Exception(ex.Message, ex)
    End Try
    End Function
    ''' <summary>
    ''' Add Years to Current Persian Date.
    ''' </summary>
    ''' <param name="years"></param>
    ''' <returns></returns>
    Public Function AddYears(years As Integer) As PersianDate
    Try
    Return New PersianDate(Me.ToDateTime.AddYears(years))
    Catch ex As Exception
    Throw New Exception(ex.Message, ex)
    End Try
    End Function
    ''' <summary>
    ''' Convert Current PersionDate to Gregorian Date.
    ''' </summary>
    ''' <returns></returns>
    Public Function ToDateTime() As Date
    Return New Date(Year, Month, DayOfMonth, Hour, Minute, Second, MilliSecond, PCalendar)
    End Function
    End Class
    آخرین ویرایش به وسیله ROSTAM2 : جمعه 13 آبان 1401 در 09:06 صبح

تاپیک های مشابه

  1. مبتدی: اموزش ساخت قالب فارسی و ریسپانسیو وردپرسی رو کسی داره ؟
    نوشته شده توسط mohammadreza65 در بخش PHP
    پاسخ: 4
    آخرین پست: چهارشنبه 26 آبان 1400, 10:55 صبح
  2. پاسخ: 7
    آخرین پست: پنج شنبه 31 اردیبهشت 1394, 20:28 عصر
  3. پاسخ: 0
    آخرین پست: چهارشنبه 30 بهمن 1392, 09:31 صبح
  4. سورس فارسی نویس، نوشتن فارسی در برنامه هایی که زبان فارسی رو پشتیبانی نمیکنند
    نوشته شده توسط سید حمید حق پرست در بخش برنامه نویسی در 6 VB
    پاسخ: 6
    آخرین پست: چهارشنبه 02 بهمن 1392, 10:00 صبح

برچسب های این تاپیک

قوانین ایجاد تاپیک در تالار

  • شما نمی توانید تاپیک جدید ایجاد کنید
  • شما نمی توانید به تاپیک ها پاسخ دهید
  • شما نمی توانید ضمیمه ارسال کنید
  • شما نمی توانید پاسخ هایتان را ویرایش کنید
  •