[FUNC] Petite fonction : _YDay_To_Date

Partagez des fonctions et des UDF AutoIt.
Règles du forum
.
Répondre
Avatar du membre
GhostLine
Niveau 7
Niveau 7
Messages : 342
Enregistré le : jeu. 12 mars 2009 15:28
Localisation : Paris
Status : Hors ligne

[FUNC] Petite fonction : _YDay_To_Date

#1

Message par GhostLine »

Salut à tous !

J'ai codé ce matin une petite fonction dont j'avais besoin pour mon boulot : vu qu'elle n'a pas l'air de trop mal fonctionner, je partage :)

Il s'agit d'une fonction qui permet de donner la date du xxxème jour d'une année donnée. Par exemple,
_YDay_To_Date("90", "2015") nous donnera "2015/03/31", soit aujour'hui, qui se trouve être ... le 90ème jour de 2015 :)

N'hésitez pas à corriger mes erreurs, je suis preneur.

Code : Tout sélectionner

#include <Date.au3>

MsgBox(0,"",_YDay_To_Date("90", "2015"))


; #FUNCTION# ====================================================================================================================
; Name ..........: YDay_To_Date
; Description ...: Give the date of the xxxth day of a given year
; Syntax ........: YDay_To_Date($yday, $year)
; Parameters ....: $yday    : the day number you want to "convert" to date
;                : $year    : the concerned year
; Return values .: If there's no error, the date in standard format (YYYY/MM/DD) ; otherwise, nothing.
; Author ........: GhostLine
; Example .......: Yes
; ===============================================================================================================================
Func _YDay_To_Date($yday, $year)
    If _DateIsLeapYear($year) And $yday > 364 Or $yday > 365 Then Exit
    $month = 1
    For $day = 1 To $yday
        If $day = $yday Then ExitLoop
        If _DateIsValid($year & "/" & StringRight(0 & $month, 2) & "/" & StringRight(0 & $day, 2)) <> 1 Then
            $yday = $yday - ($day - 1)
            $day = 1
            $month = $month + 1
        EndIf
    Next
    Return($year & "/" & StringRight(0 & $month, 2) & "/" & StringRight(0 & $yday, 2))
EndFunc   ;==>_YDay_To_Date
Modifié en dernier par GhostLine le mar. 31 mars 2015 13:18, modifié 1 fois.
Avatar du membre
jchd
AutoIt MVPs (MVP)
AutoIt MVPs (MVP)
Messages : 2272
Enregistré le : lun. 30 mars 2009 22:57
Localisation : Sud-Ouest de la France (43.622788,-1.260864)
Status : Hors ligne

Re: [..] Petite fonction : _YDay_To_Date

#2

Message par jchd »

Tu peux faire plus compact et éviter de boucler sur le quantième.

Code : Tout sélectionner

#include <Date.au3>

Local $y, $q

_DateIsoToQuant("2014-12-31", $y, $q)
ConsoleWrite($y & "-" & $q & @LF)

_DateIsoToQuant("2015-03-31", $y, $q)
ConsoleWrite($y & "-" & $q & @LF)

ConsoleWrite(_DateQuantToISO(2014, 365) & @LF)

ConsoleWrite(_DateQuantToISO(2015, 90) & @LF)

Func _DateIsoToQuant($sDate, ByRef $iYear, ByRef $iQuant)
    $iYear = Int(StringLeft($sDate, 4))
    $iQuant = _DateDiff("D", StringFormat("%04i-12-31", $iYear - 1), $sDate)
EndFunc

Func _DateQuantToISO($iYear, $iQuant)
    Return _DateAdd("D", $iQuant, StringFormat("%04i-12-31", $iYear - 1))
EndFunc
 
La cryptographie d'aujourd'hui c'est le taquin plus l'électricité.
Avatar du membre
GhostLine
Niveau 7
Niveau 7
Messages : 342
Enregistré le : jeu. 12 mars 2009 15:28
Localisation : Paris
Status : Hors ligne

Re: [..] Petite fonction : _YDay_To_Date

#3

Message par GhostLine »

C'est vrai que je suis très c*n ... Je viens de réécrire une portion de la fonction _DateAdd :D

La prochaine fois, je chercherais mieux, je n'avais pas vu cette fonction :)
Répondre