Coder Social home page Coder Social logo

jdate's Introduction

JDate

Build Status NPM Version NPM License

A Jalali to Gregorian converter in JavaScript with support of formatting output

Installation

Install via NPM/Yarn:

npm install jalali-date

You could grab the latest version from lib directory and use it:

<head>
  <script src="jdate.js" type="text/javascript" charset="utf-8"></script>
  <script src="jdate.min.js" type="text/javascript" charset="utf-8"></script>
</head>

The full-version is useful for debugging. You may want to use minified version in production as it is smaller.

Initialization

For initializing JDate you may either pass an array of Jalali date to it or a Date object. If no parameter is passed, the default is today:

const JDate = require('jalali-date');
const jdate = new JDate; // => default to today
const jdate2 = new JDate(1393, 10, 11);
const jdate3 = new JDate([1393, 10, 11]);
const jdate4 = new JDate(new Date(2014, 1, 3));

API

jdate.date //=> [1393, 5, 13] An Array of Jalali Date
jdate._d // => Gregorian Date Object

// Getters
jdate.getFullYear() // => 1393
jdate.getMonth() // => 5
jdate.getDate() // => 13
jdate.getDay() // => 1

// Setters
jdate.setFullYear(1394)
jdate.setMonth(6)
jdate.setDate(12)

// Formatting output
jdate.format('dddd DD MMMM YYYY') // => پنج‌شنبه 12 شهریور 1394

// Static functions
JDate.isLeapYear(1393) // => false
JDate.daysInMonth(1393, 5) // => 31
JDate.toGregorian(1393, 12, 11) // => Gregorian Date object
JDate.toJalali(new Date) // => JDate object

Formatting output

Use format() and following conversion identifiers as follows:

date.format('dddd DD MMMM YYYY') //=> دوشنبه 6 امرداد 1393

The conversion identifiers are as follows:

Identifier Description Example
YYY or YYYY Full Year (4 digits) 1393
YY Year (2 digits) 93
M Month in number returns 5 for امرداد
MM Month in number returns 05 for امرداد
MMM or MMMM Month in string امرداد
D Day in number 26
DD Day in number 06
d or dd Abbreviation of day name in string ۱ش (for یکشنبه)
ddd or dddd Full day name in string یکشنبه

Contribute

Report bugs and suggest feature in issue tracker. Feel free to Fork and send Pull Requests.

License

MIT

jdate's People

Contributors

arashm avatar dependabot[bot] avatar rezafarhadian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

jdate's Issues

use with Node.js

Hi,
How can use this module in Node.js?
when include this module in Node, get this error message:
Cannot find module 'jdate'

wrong leap for 1403/12

Salam,
Thanks for the great work.
Something is wrong with leapPersian. 1403 is leap but the function returns false.

Spelling mistake

hello
in MONTH_NAMES array written 'امرداد' instead of 'مرداد'

JDate() !== Date()

سلام. بنظرم توی تقویم‌های جلالی جاوااسکریپت، این تقویم از همه بهتره. من متوجه شدم که شما تلاش کردید اسم متودهای خروجی رو مثل Date نیتیو خود جاوااسکریپت انتخاب کنید، که بنظرم خیلی کار خوبیه. اما هنوز بعضی رفتارهای این دو تقویم با هم فرق دارن: مثلا ایندکس شروع ماه Date از 0 هست، (مثلا ماه Feb که ماه دوم میلادیه، برابر با 1ـه) اما توی این تقویم ماه‌ها از 1 شروع میشن. یا موقع ساخت یک آبجکت Date، می‌تونیم پارامتر ورودی رو بصورت آرگومنت، آرایه یا استرینگ پاس بدیم، اما اینجا پارامتر ورودی نسبتا محدوده و فقط آرایه و یا آبجکت Date بعنوان ورودی ساپورت میشه.
new Date(2017, 1, 1).getMonth() // 1
new Date([2017, 1, 1]).getMonth() // 0
new JDate(1396, 1, 1) // fails
new JDate([1396, 1, 1]).getMonth() // 1 ?
new Date(2017, 1, 0) // 31 Jan 2017
new JDate([1396, 5, 1]) // 0 Mordad 1396 ??

parse string to int before convert

Hi , thanks for great work.
In convert toGregorian function it is better to have parseInt on parameters.

jdate.toGregorian("1393","12","11").getTime();    // NaN 

but :

jdate.toGregorian(1393,12,11).getTime();    // 1425241800000

update for angular2

hi,
do you update this package for angular 2? or how i could use it in angular 2?
thanks

ماژول اضافی

سلام.
ماژول که تو کامیت قبلی اضاف کردم و ماژول که کلا از اول توی اسکریپت گذاشتید اصلا توی مخزن ماژول های ند نیست و جاش یه ماژول دیگه س که کلا کارش یچیزه دیگه هستش در نتیجه موقع استفاده در ند اررور میده 😐
map
خیلی راحته البته جایگزین کردنش، یعنی حتی لازم نیست ازش استفاده کنین وقتی جاوااسکریپت خودش میتونه این کارو بکنه
Array.prototype.map()

var arr = ['1', '2', '3'];
arr = arr.map(function (n) {
  return parseInt(n);
});
// [1, 2, 3]

Gregorian to Jalali

Hi,
I need to display the date stored in the Gregorian database in Jalali. How can I do this using your library? My codes are as follows:

elseif($column_type_lookup[$column_name] == 'date'){ //date with format MM/DD/YYYY
if(!empty($row[$column_name]) && ($row[$column_name] != '0000-00-00')){
$form_data[$i][$j] = date('M d, Y',strtotime($row[$column_name]));
}

				if($column_name == 'date_created' || $column_name == 'date_updated'){
					$form_data[$i][$j] = mf_short_relative_date($row[$column_name]);
				}
			}elseif($column_type_lookup[$column_name] == 'europe_date'){ //date with format DD/MM/YYYY
				
				if(!empty($row[$column_name]) && ($row[$column_name] != '0000-00-00')){
					$form_data[$i][$j]  = date('d M Y',strtotime($row[$column_name]));
				}
			}

Convert Jalali to Gregorian

My system info

  • nodejs: 14.15.0
  • jalali-date: 1.1.2

what i expect:

Conver the Jalali to Gregorian when i write this code:

let JDate = require('jalali-date');
let temp = new JDate(1399, 11, 22);

what happened:

It is behind, One day!

let JDate = require('jalali-date');
let temp = new JDate(1399, 11, 22);
console.log(temp._d); // 2021-02-09T20:30:00.000Z
console.log(new Date); //2021-02-10T07:46:28.132Z

why is this happening?

Map module

Hi,
In jalali-date/lib/jdate.js:7 you're requiring map in a try/catch.
Why you required two modules? Why In a try/catch?
And why didn't you add map and map-component in dependencies/devDependencies in package.json? (The bundling fails using browserify)

Best

npm 404! (module not found)

سلام.
ظاهرا ماژول روی مخزن ماژول های ند قرار نگرفته
طبق اطلاعات پکیج همچین دستوری باید وارد بشه
npm install JDate
و روی این آدرس: https://www.npmjs.org/package/JDate
که ظاهرا همچین چیزی وجود نداره یا هنوز ثبت نشده
لطفا رسیدگی کنین.
ممنون.

Extend format function

سلام!
اگه بشه گزینه های فرمت رو بیشتر بسازید که خیلی عالی میشه!

مثلا فعلا که تنها
Month
Day
Year
رو فرمت میکنه اگه بتونیم
Hour
Minute
Second
Timezone
Offset
رو هم فرمت کنیم اینطوری کارایی بیشتر و کاملتری میتوانیم ازش بگیریم.

یک جهان ممنون از شما!

Incorrect Leap Year Calculation and Days in Month for Non-Leap Years

The JDate class appears to have issues with calculating leap years and the number of days in a month for non-leap years.

Expected Behavior:

JDate.isLeapYear(1403) should return false.
JDate.daysInMonth(1403, 12) should return 30.

Actual Behavior:

JDate.isLeapYear(1403) returns true.
JDate.daysInMonth(1403, 12) returns 29.

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.