  | 
              
	              
	                
  
    |   | 
   
  
    
        
        การคำนวณอายุที่เราเลือกวันเกิดจาก DateTimePicker ให้แสดงออกมาทาง textbox ว่า อายุ กีปี กีเดือน กี่วัน     | 
   
  
    |   | 
   
 
 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code ที่แสดงให้นี้อาจมี Error ถ้ากำหนดวันที่เป็นอนาคตนะครับ ต้องไปกำหนด MaximumDate เป็น Date.Today ที่ตัว DateTimePicker ก่อนนะครับ 
 
Code (VB.NET) 
Public Class Form1
    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        Dim birthDayThisYear As New Date(Date.Today.Year, Me.DateTimePicker1.Value.Month, Me.DateTimePicker1.Value.Day)
        Dim years = Date.Today.Year - Me.DateTimePicker1.Value.Year
        Dim months = Date.Today.Month - Me.DateTimePicker1.Value.Month
        Dim days = Date.Today.Day - Me.DateTimePicker1.Value.Day
        If birthDayThisYear > Date.Today Then
            years -= 1
            months += 12
        End If
        If birthDayThisYear.Day > Date.Today.Day Then
            Dim dt As New Date(birthDayThisYear.Year, DateTime.Today.Month - 1, birthDayThisYear.Day)
            Dim ts As TimeSpan = Date.Today - dt
            months -= 1
            days = ts.Days
        End If
        Me.TextBox1.Text = String.Format("อายุ {0} ปี {1} เดือน {2} วัน", years.ToString, months.ToString, days.ToString)
    End Sub
End Class
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-01-06 00:06:34 | 
                        By :
                            gunnermontana | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 มันเลือกแล้ว Error ที่  
 Dim dt As New Date(birthDayThisYear.Year, DateTime.Today.Month - 1, birthDayThisYear.Day) 
 
 
Error ---->  Year, Month, and Day parameters describe an un-representable DateTime.                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-01-06 09:25:56 | 
                        By :
                            TTcom | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (VB.NET) 
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
       
     
        Dim y1 As New DateTime
        Dim y2 As New DateTime
        y2 = DateTime.Now.ToString("dd/MM/yyyy")
        y1 = DateTimePicker1.Value.ToString("dd/MM/yyyy")
        Dim year As Integer = DateDiff(DateInterval.Year, CDate(y1), CDate(y2))
        TextBox1.Text = year
        Exit Sub
End Sub
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-01-06 11:20:52 | 
                        By :
                            noh | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (C#) 
public class DateDifference
        {
            private DateTime _FromDate;
            private DateTime _ToDate;
            private int _Year = 0;
            private int _Month = 0;
            private int _Day = 0;
            public int Year
            {
                get { return _Year; }
                set { _Year = value; }
            }
            public int Month
            {
                get { return _Month; }
                set { _Month = value; }
            }
            public int Day
            {
                get { return _Day; }
                set { _Day = value; }
            }
            public DateTime FromDate
            {
                get { return _FromDate; }
                set { _FromDate = value; }
            }
            public DateTime ToDate
            {
                get { return _ToDate; }
                set { _ToDate = value; }
            }
            /// <summary>
            /// defining Number of days in month; index 0=> january and 11=> December
            /// february contain either 28 or 29 days, that's why here value is -1
            /// which wil be calculate later.
            /// </summary>
            private int[] monthDay = new int[12] { 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            
            public DateDifference(DateTime argFromDate)
            {
                _ToDate = DateTime.Now;
                _FromDate = argFromDate;
                CalcDifferenceDate();
            }
            public DateDifference(DateTime argFromDate, DateTime argToDate)
            {
                _ToDate = argToDate;
                _FromDate = argFromDate;
                CalcDifferenceDate();
            }
            private void SwapDate(ref DateTime LeftDate, ref DateTime RightDate)
            {
                DateTime tempDate;
                if (LeftDate > RightDate)
                {
                    tempDate = LeftDate;
                    LeftDate = RightDate;
                    RightDate = tempDate;
                }
                else
                {
                    LeftDate = LeftDate;
                    RightDate = RightDate;
                }
            }
            private void CalcDifferenceDate()
            {
                SwapDate(ref _FromDate, ref _ToDate);
                int carryFlag = 0;
                // ***************************
                // Day calculation 
                if (this.FromDate.Day > this.ToDate.Day)
                    carryFlag = this.monthDay[this.FromDate.Month - 1];
                // febuary detect
                if (carryFlag == -1)
                {
                    if (CheckedLeapYear(this.FromDate))
                        // leap year february contain 29 days
                        carryFlag = 29;
                    else
                        carryFlag = 28;
                }
                if (carryFlag != 0)
                {
                    this.Day = (this.ToDate.Day + carryFlag) - this.FromDate.Day;
                    carryFlag = 1;
                }
                else
                    this.Day = this.ToDate.Day - this.FromDate.Day;
                // ***************************
                // Month calculation 
                if ((this.FromDate.Month + carryFlag) > this.ToDate.Month)
                {
                    this.Month = (this.ToDate.Month + 12) - (this.FromDate.Month + carryFlag);
                    carryFlag = 1;
                }
                else
                {
                    this.Month = this.ToDate.Month - (this.FromDate.Month + carryFlag);
                    carryFlag = 0;
                }
                this.Year = this.ToDate.Year - (this.FromDate.Year + carryFlag);
            }
            private bool CheckedLeapYear(DateTime checkedDate)
            {
                int myYear = checkedDate.Year;
                return (((myYear % 4) == 0) && ((myYear % 100) != 0) || ((myYear % 400) == 0));
            }
            public string ToString(string argYearUnit, string argMonthUnit, string argDayUnit)
            {
                string retStr = string.Empty;
                if (this.Year > 0)
                    retStr = retStr + string.Format("{0} {1} ", this.Year.ToString("#,##0"), argYearUnit);
                if (this.Month > 0)
                    retStr = retStr + string.Format("{0} {1} ", this.Month.ToString("#,##0"), argMonthUnit);
                if (this.Day > 0)
                    retStr = retStr + string.Format("{0} {1} ", this.Day.ToString("#,##0"), argDayUnit);
                return retStr.Trim();
            }
            public override string ToString()
            {
                return this.ToString("ปี", "เดือน", "วัน");
            }
        }
DateDifference d = new DateDifference(Convert.ToDateTime(lbstart.Text), DateTime.Now);///ตอนเรียกใช้
                lbtotaldate.Text = d.ToString();
 
 
อันนี้ได้ปะ ผมเอาไว้ใช้คำนวณเวลา ของพนักงาน ว่าคนนี้ทำมากี่ เดือน กี่วัน กี่ปีแล้ว โค้ดผมได้มาจากเวปนี้แหละ ลองเล่นดูครับ 
ผมเอาโค้ดนี้มาตอนแรก นั่งงงแทบตาย ไล่ดูดีๆ จะเข้าใจเองครับ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-01-06 11:31:23 | 
                        By :
                            patzy | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Try it . 
 
Code (VB.NET) 
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
       
     
        Dim y1 As New DateTime
        Dim y2 As New DateTime
        y2 = DateTime.Now.ToString("MM/dd/yyyy")
        y1 = DateTimePicker1.Value.ToString("MM/dd/yyyy")
        Dim year As Integer = DateDiff(DateInterval.Year, CDate(y1), CDate(y2))
        Dim Month As Integer = DateDiff(DateInterval.Month, CDate(y1), CDate(y2))
        Dim Day As Integer = DateDiff(DateInterval.Day, CDate(y1), CDate(y2))
        TextBox1.Text = year & ":" & Month & ":" & Day
        Exit Sub
End Sub
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-01-06 11:41:51 | 
                        By :
                            noh | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 คุณ : patzy     มันเป็น C# ป่าว 
 
ส่วนของคุณ   noh   
 
Conversion from string "01/13/2555" to type 'Date' is not valid.   รูปแบบวันที่น่าจะไม่เหมือนกัน 
 
 
ตอนนี้ผมใช้  มันแสดงแค่ปี ออกมา ไม่มีเดือน วัน   ต้องทำไง  
 TxtAge.Text = DateDiff(DateInterval.Year, DTP1.Value, Now) 
 
อยากได้ที่เป็นแบบ อายุ 21 ปี 9 เดือน 5 วัน ประมาณนี้  ไม่ใช้เดือนหรือวันทั้งหมด                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-01-06 13:13:57 | 
                        By :
                            TTcom | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 แปลงเป็น VB.NET ให้ครับ 
 
Code (VB.NET) 
Public Class DateDifference
	Private _FromDate As DateTime
	Private _ToDate As DateTime
	Private _Year As Integer = 0
	Private _Month As Integer = 0
	Private _Day As Integer = 0
	Public Property Year() As Integer
		Get
			Return _Year
		End Get
		Set
			_Year = value
		End Set
	End Property
	Public Property Month() As Integer
		Get
			Return _Month
		End Get
		Set
			_Month = value
		End Set
	End Property
	Public Property Day() As Integer
		Get
			Return _Day
		End Get
		Set
			_Day = value
		End Set
	End Property
	Public Property FromDate() As DateTime
		Get
			Return _FromDate
		End Get
		Set
			_FromDate = value
		End Set
	End Property
	Public Property ToDate() As DateTime
		Get
			Return _ToDate
		End Get
		Set
			_ToDate = value
		End Set
	End Property
	''' <summary>
	''' defining Number of days in month; index 0=> january and 11=> December
	''' february contain either 28 or 29 days, that's why here value is -1
	''' which wil be calculate later.
	''' </summary>
	Private monthDay As Integer() = New Integer(11) {31, -1, 31, 30, 31, 30, _
		31, 31, 30, 31, 30, 31}
	Public Sub New(argFromDate As DateTime)
		_ToDate = DateTime.Now
		_FromDate = argFromDate
		CalcDifferenceDate()
	End Sub
	Public Sub New(argFromDate As DateTime, argToDate As DateTime)
		_ToDate = argToDate
		_FromDate = argFromDate
		CalcDifferenceDate()
	End Sub
	Private Sub SwapDate(ByRef LeftDate As DateTime, ByRef RightDate As DateTime)
		Dim tempDate As DateTime
		If LeftDate > RightDate Then
			tempDate = LeftDate
			LeftDate = RightDate
			RightDate = tempDate
		Else
			LeftDate = LeftDate
			RightDate = RightDate
		End If
	End Sub
	Private Sub CalcDifferenceDate()
		SwapDate(_FromDate, _ToDate)
		Dim carryFlag As Integer = 0
		' ***************************
		' Day calculation 
		If Me.FromDate.Day > Me.ToDate.Day Then
			carryFlag = Me.monthDay(Me.FromDate.Month - 1)
		End If
		' febuary detect
		If carryFlag = -1 Then
			If CheckedLeapYear(Me.FromDate) Then
				' leap year february contain 29 days
				carryFlag = 29
			Else
				carryFlag = 28
			End If
		End If
		If carryFlag <> 0 Then
			Me.Day = (Me.ToDate.Day + carryFlag) - Me.FromDate.Day
			carryFlag = 1
		Else
			Me.Day = Me.ToDate.Day - Me.FromDate.Day
		End If
		' ***************************
		' Month calculation 
		If (Me.FromDate.Month + carryFlag) > Me.ToDate.Month Then
			Me.Month = (Me.ToDate.Month + 12) - (Me.FromDate.Month + carryFlag)
			carryFlag = 1
		Else
			Me.Month = Me.ToDate.Month - (Me.FromDate.Month + carryFlag)
			carryFlag = 0
		End If
		Me.Year = Me.ToDate.Year - (Me.FromDate.Year + carryFlag)
	End Sub
	Private Function CheckedLeapYear(checkedDate As DateTime) As Boolean
		Dim myYear As Integer = checkedDate.Year
		Return (((myYear Mod 4) = 0) AndAlso ((myYear Mod 100) <> 0) OrElse ((myYear Mod 400) = 0))
	End Function
	Public Overloads Function ToString(argYearUnit As String, argMonthUnit As String, argDayUnit As String) As String
		Dim retStr As String = String.Empty
		If Me.Year > 0 Then
			retStr = retStr & String.Format("{0} {1} ", Me.Year.ToString("#,##0"), argYearUnit)
		End If
		If Me.Month > 0 Then
			retStr = retStr & String.Format("{0} {1} ", Me.Month.ToString("#,##0"), argMonthUnit)
		End If
		If Me.Day > 0 Then
			retStr = retStr & String.Format("{0} {1} ", Me.Day.ToString("#,##0"), argDayUnit)
		End If
		Return retStr.Trim()
	End Function
	Public Overrides Function ToString() As String
		Return Me.ToString("ปี", "เดือน", "วัน")
	End Function
End Class
Dim d As New DateDifference(Convert.ToDateTime(lbstart.Text), DateTime.Now)
lbtotaldate.Text = d.ToString()
                        
               
               | 
             
            
              
			  			  
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-01-06 13:25:19 | 
                        By :
                            webmaster | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ขอบคุณคับ เดี่ยวลองไล่ดู                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-01-06 13:31:44 | 
                        By :
                            TTcom | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |