Meilleur auteur de réponses
Ajouter le nombre de jours restant pour le prochain Anniversaire

Question
-
En VB2010 MERCI
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click MessageBox.Show(QuelAge(Me.TextBox1.Text) & " " & "ans") End Sub Public Function QuelAge(dtBirth As Date) As Integer If DateDiff(DateInterval.Day, CDate(dtBirth.Day & "/" & dtBirth.Month & "/" & Now.Year), Now) >= 0 Then QuelAge = DateDiff(DateInterval.Year, dtBirth, Now) Else QuelAge = DateDiff(DateInterval.Year, dtBirth, Now) - 1 End If End Function
- Type modifié Aurel Bera lundi 28 janvier 2013 08:53 Question
- Modifié Aurel Bera lundi 28 janvier 2013 08:57 titre
Réponses
-
Par exemple, la classe :
Public Class BirthDay Dim dtBirth As Date ReadOnly Property DateValide(dtText As String) As Boolean Get DateValide = IsDate(dtText) If DateValide Then dtBirth = CDate(dtText) End If End Get End Property Property DateNaissance() As Date Set(value As Date) dtBirth = value End Set Get DateNaissance = dtBirth End Get End Property ReadOnly Property QuelAge() As Integer Get If DateDiff(DateInterval.Day, CDate(dtBirth.Day & "/" & dtBirth.Month & "/" & Now.Year), Now) >= 0 Then QuelAge = DateDiff(DateInterval.Year, dtBirth, Now) Else QuelAge = DateDiff(DateInterval.Year, dtBirth, Now) - 1 End If End Get End Property ReadOnly Property Jours2Anniv() As Integer Get If DateDiff(DateInterval.Day, CDate(dtBirth.Day & "/" & dtBirth.Month & "/" & Now.Year), Now) >= 0 Then Jours2Anniv = DateDiff(DateInterval.Day, Now, CDate(dtBirth.Day & "/" & dtBirth.Month & "/" & Now.Year + 1)) Else Jours2Anniv = DateDiff(DateInterval.Day, Now, CDate(dtBirth.Day & "/" & dtBirth.Month & "/" & Now.Year)) End If End Get End Property End Class
Et dans une feuille :
Public Class Form1 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim dtBirth As New BirthDay If dtBirth.DateValide(Me.TextBox1.Text) Then MessageBox.Show(dtBirth.QuelAge & " " & "ans" & vbCrLf & _ "Prochain anniversaire dans : " & dtBirth.Jours2Anniv & " jour(s)") Else MessageBox.Show("Format de date : '" & Me.TextBox1.Text & "' invalide" & vbCrLf & _ "Veuillez saisir : jj/mm/aaaa") Me.TextBox1.Focus() End If End Sub End Class
Cordialement, Jacques
- Marqué comme réponse Aurel Bera lundi 28 janvier 2013 08:54
Toutes les réponses
-
Bonjour,
Utiliser l'intervalle "Day'
NonbreDeJour = DateDiff(DateInterval.Day,DATE1,DATE2)
Jacques
Si la réponse vous satisfait, n'oubliez pas de la proposer comme réponse. Merci
- Modifié Paraglider dimanche 27 janvier 2013 10:57
-
Bonjour rocdan13,
Tu as retrouvé la fonction 'QuelAge' semble t-il, mais ce n'est pas nécessaire de multiplier les fils.Je remet le code complet, avec le décompte pour le prochain anniversaire (vu mon âge, je suis moins pressé que toi :-) ) :
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim dtBirth As Date If IsDate(Me.TextBox1.Text) Then dtBirth = CDate(Me.TextBox1.Text) MessageBox.Show(QuelAge(dtBirth) & " " & "ans" & vbCrLf & _ "Prochain anniversaire dans : " & Jours2Anniv(dtBirth) & " jour(s)") Else MessageBox.Show("Format de date : '" & Me.TextBox1.Text & "' invalide" & vbCrLf & _ "Veuillez saisir : jj/mm/aaaa") Me.TextBox1.Focus() End If End Sub Public Function QuelAge(dtBirth As Date) As Integer If DateDiff(DateInterval.Day, CDate(dtBirth.Day & "/" & dtBirth.Month & "/" & Now.Year), Now) >= 0 Then QuelAge = DateDiff(DateInterval.Year, dtBirth, Now) Else QuelAge = DateDiff(DateInterval.Year, dtBirth, Now) - 1 End If End Function Public Function Jours2Anniv(dtBirth As Date) As Integer If DateDiff(DateInterval.Day, CDate(dtBirth.Day & "/" & dtBirth.Month & "/" & Now.Year), Now) >= 0 Then Jours2Anniv = DateDiff(DateInterval.Day, Now, CDate(dtBirth.Day & "/" & dtBirth.Month & "/" & Now.Year + 1)) Else Jours2Anniv = DateDiff(DateInterval.Day, Now, CDate(dtBirth.Day & "/" & dtBirth.Month & "/" & Now.Year)) End If End Function
Cordialement, Jacques
-
-
Par exemple, la classe :
Public Class BirthDay Dim dtBirth As Date ReadOnly Property DateValide(dtText As String) As Boolean Get DateValide = IsDate(dtText) If DateValide Then dtBirth = CDate(dtText) End If End Get End Property Property DateNaissance() As Date Set(value As Date) dtBirth = value End Set Get DateNaissance = dtBirth End Get End Property ReadOnly Property QuelAge() As Integer Get If DateDiff(DateInterval.Day, CDate(dtBirth.Day & "/" & dtBirth.Month & "/" & Now.Year), Now) >= 0 Then QuelAge = DateDiff(DateInterval.Year, dtBirth, Now) Else QuelAge = DateDiff(DateInterval.Year, dtBirth, Now) - 1 End If End Get End Property ReadOnly Property Jours2Anniv() As Integer Get If DateDiff(DateInterval.Day, CDate(dtBirth.Day & "/" & dtBirth.Month & "/" & Now.Year), Now) >= 0 Then Jours2Anniv = DateDiff(DateInterval.Day, Now, CDate(dtBirth.Day & "/" & dtBirth.Month & "/" & Now.Year + 1)) Else Jours2Anniv = DateDiff(DateInterval.Day, Now, CDate(dtBirth.Day & "/" & dtBirth.Month & "/" & Now.Year)) End If End Get End Property End Class
Et dans une feuille :
Public Class Form1 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim dtBirth As New BirthDay If dtBirth.DateValide(Me.TextBox1.Text) Then MessageBox.Show(dtBirth.QuelAge & " " & "ans" & vbCrLf & _ "Prochain anniversaire dans : " & dtBirth.Jours2Anniv & " jour(s)") Else MessageBox.Show("Format de date : '" & Me.TextBox1.Text & "' invalide" & vbCrLf & _ "Veuillez saisir : jj/mm/aaaa") Me.TextBox1.Focus() End If End Sub End Class
Cordialement, Jacques
- Marqué comme réponse Aurel Bera lundi 28 janvier 2013 08:54
-
-
-
Je crois, comme le dit Troxsa, qu'il faut passer le thread de 'Discussion générale' à 'Question' pour avoir le choix 'Proposer comme réponse'.
Je pense qu'Aurel Bera est habilité à le faire, peut être aussi l'auteur ?
Sinon se sera pour la prochaine fois ...
Cordialement, Jacques
-
-
-
Bonjour
J’ai transformée ce thread en question et j’ai marqué la réponse.
Cordialement,
- Marqué comme réponse rocdan13 lundi 28 janvier 2013 16:49
- Non marqué comme réponse Aurel Bera lundi 28 janvier 2013 17:01