Skip to content

Instantly share code, notes, and snippets.

@lekhnath lekhnath/app.js
Last active Aug 6, 2017

Embed
What would you like to do?
BS to AD converter and vice versa
var BS_MONTHS_FULL_NE = ['वैशाख', 'जेठ', 'असार', 'साउन', 'भदौ', 'असोज', 'कात्तिक', 'मंसिर', 'पुष', 'माघ', 'फागुन', 'चैत'];
var BS_DAYS_FULL_NE = ['आईतबार', 'सोमबार', 'मंगलबार', 'बुधबार', 'बिहीबार', 'शुक्रबार', 'शनिबार'];
function convertBs2Ad(e){
e.preventDefault();
var form, bsDate, adDate;
try {
form = document.forms['bs2adForm'];
bsDate = {
year: parseInt(form.year.value),
month: parseInt(form.month.value),
date: parseInt(form.date.value)
};
adDate = convertDate.bs2ad(bsDate);
document.getElementById('adOutput').innerHTML = adDate.toDateString();
}catch (e) {
alert('Sorry! try some other date!');
}
}
function convertAd2Bs(e){
e.preventDefault();
var form, bsDate, adDate;
try{
form = document.forms['ad2bsForm'];
adDate = new Date(parseInt(form.year.value), parseInt(form.month.value) - 1, parseInt(form.date.value));
bsDate = convertDate.ad2bs(adDate);
document.getElementById('bsOutput').innerHTML = [
BS_DAYS_FULL_NE[adDate.getDay()],
BS_MONTHS_FULL_NE[bsDate.month - 1],
bsDate.date,
bsDate.year
].join(' ');
} catch(e) {
alert('Sorry! try some other date!');
}
}
window.onload = function onDocumentReady(){
var bs2adForm = document.forms['bs2adForm'];
var ad2bsForm = document.forms['ad2bsForm'];
bs2adForm.year.addEventListener('keyup', autoTab(bs2adForm, 4, 'month'));
bs2adForm.month.addEventListener('keyup', autoTab(bs2adForm, 2, 'date'));
bs2adForm.date.addEventListener('keyup', autoTab(bs2adForm, 2, 'submit'));
ad2bsForm.year.addEventListener('keyup', autoTab(ad2bsForm, 4, 'month'));
ad2bsForm.month.addEventListener('keyup', autoTab(ad2bsForm, 2, 'date'));
ad2bsForm.date.addEventListener('keyup', autoTab(ad2bsForm, 2, 'submit'));
function autoTab(form, len, nextControlName) {
return function (e) {
if( e.which < 96 || e.which > 105){return;}
if(e.target.value.length === len) {
form[nextControlName].focus();
}
}
}
}
var convertDate = function () {
}
convertDate.START_DATE_BS = Object.seal({ year: 1990, month: 1, date: 1 });
convertDate.START_DATE_AD = new Date(1933, 3, 12); //NOTE: VVI its a corresponding A.D. date of one day less than 'START_DATE_BS'
convertDate.SECONDS_IN_DAY = 86400;//60*60*24
convertDate.MONTHS_IN_YEAR = 12;
convertDate.BS_DAYS_IN_YEAR = {
"1990": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"1991": [31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30],
"1992": [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
"1993": [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
"1994": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"1995": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
"1996": [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
"1997": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"1998": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"1999": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
"2000": [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
"2001": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2002": [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
"2003": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
"2004": [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
"2005": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2006": [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
"2007": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
"2008": [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31],
"2009": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2010": [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
"2011": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
"2012": [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
"2013": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2014": [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
"2015": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
"2016": [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
"2017": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2018": [31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30],
"2019": [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
"2020": [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
"2021": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2022": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
"2023": [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
"2024": [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
"2025": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2026": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
"2027": [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
"2028": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2029": [31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30],
"2030": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
"2031": [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
"2032": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2033": [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
"2034": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
"2035": [30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31],
"2036": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2037": [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
"2038": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
"2039": [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
"2040": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2041": [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
"2042": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
"2043": [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
"2044": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2045": [31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30],
"2046": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
"2047": [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
"2048": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2049": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
"2050": [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
"2051": [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
"2052": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2053": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
"2054": [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
"2055": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2056": [31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30],
"2057": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
"2058": [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
"2059": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2060": [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
"2061": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
"2062": [30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31],
"2063": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2064": [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
"2065": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
"2066": [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31],
"2067": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2068": [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
"2069": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
"2070": [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
"2071": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2072": [31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30],
"2073": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
"2074": [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
"2075": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2076": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
"2077": [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
"2078": [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
"2079": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2080": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
"2081": [31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30],
"2082": [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30],
"2083": [31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30],
"2084": [31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30],
"2085": [31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30],
"2086": [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30],
"2087": [31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30],
"2088": [30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30],
"2089": [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30],
"2090": [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30],
"2091": [31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30],
"2092": [30, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30],
"2093": [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30],
"2094": [31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30],
"2095": [31, 31, 32, 31, 31, 31, 30, 29, 30, 30, 30, 30],
"2096": [30, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
"2097": [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30],
"2098": [31, 31, 32, 31, 31, 31, 29, 30, 29, 30, 29, 31],
"2099": [31, 31, 32, 31, 31, 31, 30, 29, 29, 30, 30, 30],
"2100": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
"2101": [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
"2102": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2103": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
"2104": [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
"2105": [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
"2106": [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
"2107": [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30]
};
convertDate.ad2bs = function (adDate) {
var daysFromStartDateAd;
var bsDate = {year: convertDate.START_DATE_BS.year, month: convertDate.START_DATE_BS.month, date: convertDate.START_DATE_BS.month};
daysFromStartDateAd = Math.floor((adDate.getTime() - convertDate.START_DATE_AD.getTime()) / (convertDate.SECONDS_IN_DAY * 1000));
do {
var daysInBsYear;
daysInBsYear = array_sum(convertDate.BS_DAYS_IN_YEAR[bsDate.year.toString()]);
if (daysInBsYear > daysFromStartDateAd) {
for (var i = 0, daysInBsMonth; i < convertDate.MONTHS_IN_YEAR; i++) {
daysInBsMonth = convertDate.BS_DAYS_IN_YEAR[bsDate.year.toString()][i];
if (daysInBsMonth >= daysFromStartDateAd) {
//Calculate date
bsDate.date = daysFromStartDateAd;
daysFromStartDateAd = 0;
break;
} else {
//Calculate month
bsDate.month++;
daysFromStartDateAd -= daysInBsMonth;
}
}
} else {
//Calculate year
bsDate.year++;
daysFromStartDateAd -= daysInBsYear;
}
} while (daysFromStartDateAd > 0);
return bsDate;
}
convertDate.bs2ad = function (bsDate) {
var daysFromStartDateBs = 0, msFromStartDateBs;
//Add days in year
for (var i = convertDate.START_DATE_BS.year, daysInBsYear; i < bsDate.year; i++) {
daysInBsYear = array_sum(convertDate.BS_DAYS_IN_YEAR[i.toString()]);
daysFromStartDateBs += daysInBsYear;
}
//Add days in month
daysFromStartDateBs += array_sum(
convertDate.BS_DAYS_IN_YEAR[bsDate.year.toString()].slice(0, bsDate.month-1)
);
//Add date
daysFromStartDateBs += bsDate.date;
//Convert to milliseconds
msFromStartDateBs = daysFromStartDateBs * convertDate.SECONDS_IN_DAY * 1000;
return new Date(convertDate.START_DATE_AD.getTime() + msFromStartDateBs);
}
//helper functions
function array_sum( arr ) {
var initial = 0;
return arr.reduce(function (total, nth){
return total += nth;
}, initial);
}
<html>
<head>
<title>
B.S. to A.D. date conversion and vice versa
</title>
<meta charset="utf-8" />
</head>
<body>
<script src="https://gist.githubusercontent.com/lekhnath/80e60f0988a51cd6e0c055a94682e998/raw/converter.js"></script>
<script src="https://gist.githubusercontent.com/lekhnath/80e60f0988a51cd6e0c055a94682e998/raw/app.js"></script>
<form name="bs2adForm" onsubmit="convertBs2Ad(event)">
<h2> B.S. to A.D. converter</h2>
<p>
<label>
साल (Year):
</label>
<input type="number" name="year" placeholder="eg. 2049" required/>
<label>
मास (Month):
</label>
<input type="number" name="month" placeholder="eg. 01 (for Baisakh)" required/>
<label>
गते (Date):
</label>
<input type="number" name="date" placeholder="eg. 11" required/>
<button type="submit" name="submit"> Convert to Ad </button>
<strong id="adOutput"></strong>
</p>
</form>
<hr />
<form name="ad2bsForm" onsubmit="convertAd2Bs(event)">
<h2> A.D. to B.S. converter</h2>
<p>
<label>
Year:
</label>
<input type="number" name="year" placeholder="eg. 1992" required/>
<label>
Month:
</label>
<input type="number" name="month" placeholder="eg. 01 (for January)" required/>
<label>
Date:
</label>
<input type="number" name="date" placeholder="eg. 11" required/>
<button type="submit" name="submit"> Convert to Bs </button>
<strong id="bsOutput"></strong>
</p>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.