Code Monkey home page Code Monkey logo

hijri.js's Introduction

Hijri.js

A simple implementation for the Islamic calender(Hijri) in Javascript

The Problem

I made this project just because I did not found any correct and open source Hijri converter. Each time I try one its either stored in database and fetch the data each time or it does not give me the correct date "specially when I try before 1420H"

Usage

Current Day

To get Today in Hijri:

<body onload="initWork()" >
	<!-- Include Hijri.js in your work -->
	<script type="text/javascript" src="Hijri.js"></script>

	<span>Today is: </span><span id="today"></span>
	<script type="text/javascript">
	function initWork() {
		//Today
		var todayElement = document.getElementById("today");
		todayElement.innerHTML = HijriJS.today().toString();
	}
	</script>
</body>

Conversion

To convert between Hijri and Gregorian use the HijriJS.toHijri(dateString, splitter) to convert Gregorian to Hijri or HijriJS.toGregorian(dateString, splitter) to convert Hijri to Gregorian where the splitter is the symbol splits the date (e.g. for 1434/1/1 is HijriJS.toGregorian("1434/1/1", "/")):

<body>
	<!-- Include Hijri.js in your work -->
	<script type="text/javascript" src="Hijri.js"></script>

	<!-- Input Area -->
	<input id="gregorianInput" onchange="convertToHijri()" />
	&nbsp; <span id="hijriDate"></span>

	<script type="text/javascript">
	function convertToHijri() {
		var gregorianDate = document.getElementById("gregorianInput").value
		var hijriDate = document.getElementById("hijriDate");

		var date = HijriJS.toHijri(gregorianDate, "/");
		hijriDate.innerHTML = date.toString();
	}
	</script>
</body>

Formating

More advanced is to print the date with different format you can use date.toFormat(yourStringFormat); where yourStringFormat can be:

  • YYYY: For the four digit year notation e.g. 1434.
  • YY: For two year notation e.g. 34 of 1434.
  • mm: For two digit month notation e.g. 03.
  • m: For one digit month notation e.g. 3.
  • dd: For two digit day notation.
  • d: For one digit day notation.
  • n: For calender notation "H for Hijri or G for Gregorian".

You can use the same example above and change it like this:

function convertToHijri() {
	var gregorianDate = document.getElementById("gregorianInput").value
	var hijriDate = document.getElementById("hijriDate");

	var date = HijriJS.toHijri(gregorianDate, "/");
	hijriDate.innerHTML = date.toFormat("dd/mm/yyyyN");
}

hijri.js's People

Contributors

xsoh avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.