Created
September 19, 2021 15:21
-
-
Save mer30hamid/e178b58f3a0959e3a4b40d12c74d7cf4 to your computer and use it in GitHub Desktop.
An example for Gregorian to Shamsi (Jalali or Persian) date conversion in Hugo Templating language. - تبدیل تاریخ از میلادی به شمسی در هوگو
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {{ $shamsi := newScratch }} | |
| {{ $shamsi.Set "diff" (sub (add ( div .PublishDate.Unix 86400) 25569) (add ( div (time "1920-03-20").Unix 86400) 25569)) }} | |
| {{ $shamsi.Set "gn" (div (mod (mul ($shamsi.Get "diff") 100) 36525) 100) }} | |
| {{ $shamsi.Set "year" (math.Floor (add (div ($shamsi.Get "diff") 365.25) 1299)) }} | |
| {{ if lt (int ($shamsi.Get "gn")) 186 }} | |
| {{$shamsi.Set "month" (add (mod (int (div ($shamsi.Get "gn") 31)) 12) 1)}} | |
| {{else if eq (mod (int (div ($shamsi.Get "diff") 365.25)) 4) 0}} | |
| {{ $shamsi.Set "month" ( add (mod (add (int (div (sub (int ($shamsi.Get "gn")) 186) 30)) 6) 12) 1) }} | |
| {{else if lt (int ($shamsi.Get "gn")) 336 }} | |
| {{ $shamsi.Set "month" ( add (mod (add (int (div (sub (int ($shamsi.Get "gn")) 186) 30)) 6) 12) 1) }} | |
| {{else}} | |
| {{ $shamsi.Set "month" ( add (mod (add (int (div (sub (int ($shamsi.Get "gn")) 336) 29)) 11) 12) 1) }} | |
| {{ end }} | |
| {{ if lt (int ($shamsi.Get "gn")) 186 }} | |
| {{ $shamsi.Set "day" (add (mod (int ($shamsi.Get "gn")) 31) 1) }} | |
| {{else if eq (mod (int (div ($shamsi.Get "diff") 365.25)) 4) 0}} | |
| {{ $shamsi.Set "day" (add (mod (sub (int ($shamsi.Get "gn")) 186) 30) 1) }} | |
| {{else if lt (int ($shamsi.Get "gn")) 336 }} | |
| {{ $shamsi.Set "day" (add (mod (sub (int ($shamsi.Get "gn")) 186) 30) 1) }} | |
| {{else}} | |
| {{ $shamsi.Set "day" (add (mod (sub (int ($shamsi.Get "gn")) 336) 29) 1) }} | |
| {{ end }} | |
| {{ $shamsi.Get "year" }}/{{$shamsi.Get "month"}}/{{$shamsi.Get "day"}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment