Useful Universal Date Converter function
Posted: Mon Apr 13, 2015 6:14 pm
Based on adeeadee's JD function and some algorithms found on the internet to convert from Julian to standard date.
I hope you find this useful
I hope you find this useful
Code: Select all
#parameter 1: X2Y, Conversion from X to Y formats, where
#M=Mikrotik date format: dec/31/9999
#D=Standard date format: 31/12/9999
#J=Julian days: 5373484
#N=Number: 99991231
#
#parameter 2: date in the format specified above (mmm/dd/yyyy, dd/mm/yyyy, jjjjjjj, yyyymmdd)
# example: "$ConvertDate M2D apr/13/2015" would return "13/04/2015"
# example: "$ConvertDate D2N 13/04/2015" would return "20150413"
:local day;
:local month;
:local year;
:local months [:toarray "jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec"];
#from Mikrotik date
:if ([:pick $1 0 1]="M") do={
:if ($2="never") do={
:set day 31;
:set month 12;
:set year 9999;
} else= {
:set day [:pick $2 4 6 ];
:set month [:pick $2 0 3 ];
:set year [:pick $2 7 11 ];
:local mm ([:find $months $month] + 1);
:set month $mm;
};
};
#from Standard date
:if ([:pick $1 0 1]="D") do={
:set day [:tonum [:pick $2 0 2 ]];
:set month [:tonum [:pick $2 3 5 ]];
:set year [:tonum [:pick $2 6 10 ]];
};
#from Number date
:if ([:pick $1 0 1]="N") do={
:set day [:tonum [:pick $2 6 8 ]];
:set month [:tonum [:pick $2 4 6 ]];
:set year [:tonum [:pick $2 0 4 ]];
};
#from Julian days
:if ([:pick $1 0 1]="J") do={
:local Z ((([:tonum $2]*10)+5)/10);
:local alpha ((($Z*100)-186721625) / 3652425);
:local A ($Z+1+$alpha-($alpha/4));
:local B ($A+1524);
:local C ((($B*100)-12210)/36525);
:local D (($C*36525) / 100);
:local E ((10000*($B-$D))/306001);
:set day ($B-$D-(306001*$E / 10000));
:if ($E>13) do={
:set $E ($E-13);
:set $C ($C+1);
};
:set month ($E-1);
:if ($month=0 || $month=1) do={:set month ($month+1)};
:set year ($C-4716);
};
#to Standard date
:if ([:pick $1 2 3]="D") do={
:if ($month < 10) do={:set month ("0" . $month);};
:if ($day < 10) do={:set day ("0" . $day);};
:return ($day."/".$month."/".$year);
};
#to Number date
:if ([:pick $1 2 3]="N") do={
:return [:tonum ($year.$month.$day)];
};
#to Mikrotik date
:if ([:pick $1 2 3]="M") do={
:return ($months->($month-1)."/".$day."/".$year);
};
#to Julian days
:if ([:pick $1 2 3]="J") do={
:if ($month = 1 || $month = 2) do={
:set year ($year-1);
:set month ($month+12);
};
:local A ($year/100);
:local B ($A/4);
:local C (2-$A+$B);
:local D ((($year+4716) * 36525)/100);
:local E ((306001*($month+1))/10000);
:local jd ($C+$day+$D+$E-1524);
:return $jd;
};