The official calendar of our country is jalali! Jalali is a type of calendar that has a mathematical relationship with the Gregorian calendar. I want to change Date() in JS to returns jalali values. there are many lib or func for this, but I don't want use them. Can I redefine Date()? Where can I view Date() source?
redefine Date() javascript
83 views Asked by erfan bakhtiari At
2
There are 2 answers
0
RobG
On
Don't mess with objects you don't own. You can create your own date object called maybe jDate (after "jalali date", which I assume is the same as the Intl object's "persian" calendar) and implement methods there.
The Intl.DateTimeFormat constructor returns an object that has a formatToParts method that you can leverage to implement the Date methods you need, then you can work on a standard Date object underneath but return Jalali values from the methods. e.g. to get all the current date parts in English:
let f = new Intl.DateTimeFormat('en-GB-u-ca-persian',{
year: 'numeric',
month: 'long',
day: 'numeric',
weekday: 'long',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
hour12: false,
});
console.log('Current Jalali date: ' + f.format(new Date()));
console.log('The parts:');
f.formatToParts(new Date()).forEach(part => console.log(part.type + ': ' + part.value));
For some things you have to run the format method more than once with different options, e.g. to get both the month name and number as both are specified by the month option: month: 'long' for the name and month: 'numeric' for the number.
Related Questions in JAVASCRIPT
- Using Puppeteer to scrape a public API only when the data changes
- inline SVG text (js)
- An array of images and a for loop display the buttons. How to assign each button to open its own block by name?
- Storing the preferred font-size in localStorage
- Simple movie API request not showing up in the console log
- Authenticate Flask rest API
- Deploying sveltekit app with gunjs on vercel throws cannot find module './lib/text-encoding'
- How to request administrator rights?
- mp4 embedded videos within github pages website not loading
- Scrimba tutorial was working, suddenly stopped even trying the default
- In Datatables, start value resets to 0, when column sorting
- How do I link two models in mongoose?
- parameter values only being sent to certain columns in google sheet?
- Run main several times of wasm in browser
- Variable inside a Variable, not updating
Related Questions in DATE
- Using JQuery Date Slider
- Get Type Error when using .countDocuments with mongoDB
- Gsheet - Automatically send an email using a date & time in a Cell
- Market share growth versus last month, last year
- Convert date string into a number in Python
- Date formatting "yyyy-MM-dd hh:mm:ss +0000" to "dd MMM" in swift
- How does Big Query differentiate between a day and month when we upload any CSV or text file?
- Event_date reference in CTE
- Laravel Nova 4 format date in BelongsTo field
- how to count days elpased between each interval using mySQL?
- MariaDB incorrect dates
- Date issue - how do I parse date in this SQL
- Countdown to varying payday in Javascript
- Can I have a static ISO8601DateFormatter with specific formatOptions in Swift?
- Date Format mdy vs dmy
Related Questions in CALENDAR
- Tasks in a RecycleView are merging into 1 item
- Python Django HTMLCalendar How To Set First Weekday To Sunday
- Generating Google Calendar Share URL
- Vertical Line Plot based on Actual Calendar Days not based on Bar Index
- How to add event in google calendar in kotlin (android)?
- Creating a outlook calendar event leaves a shade of that event throughout the entire day till the end date
- Enable the user to pick a date from Calender for non consecutive date only in ios?
- MUI DatePicker 6.19.7 change picked day bgcolor using sx
- how to display event creation screen while adding event to the calendar?
- Displaying ICS informations in grid
- Share Outlook calendars with all members of specific security group
- How can I do this? (Calendar search, find, match then edit)
- One day difference in Converting Gregorian to Hijri date using Intl.DateTimeFormat
- Can't limit to only 3 or 4 days in week/week view in React big calendar
- "Get event happening now" type problem in Redis
Related Questions in BUILT-IN
- Debugging in Django REST Framework
- How the builtin Python's next function works as used in the Gunicorn server
- Config SVNKit like a built-in option on NetBeans 17
- How does __is_const(_Tp) in LLVM's header work?
- Does each database have their own built-in or system objects (functions, procedures, views, etc) in PostgreSQL?
- Portability of __builtin_clzll for calculating floor power of 2 and fallback options
- in Excel is it possible to use a standard function in place of a lambda?
- List of BuildIn robot keywords
- Is there a python module for identifying the source of a built in function?
- Add method to OS built-in module
- Crostini Built-In Camera Support?
- Python Extend Subclass Overload Built-In Int or List Class and object creation using Int() Brackets vs List()
- Why there is no __builtin_div_overflow function in gcc?
- PowerShell Profile Locations Built-in Directory/Folder Usage: How to Use Built-in Scripts directory?
- In the python functions `all` and `any`, what does the second undocumented argument do?
Related Questions in REDEFINE
- Overriding, redeclaring, hiding and redefining of methods in classes, interfaces and classes implementing interfaces
- Javascript class method redefine with extend original content
- How to make PHP permanently running script "forget" everything and perform almost as a restart
- redefine Date() javascript
- (C++) How to redefine "=" operator for object
- Extend a public XSD schema with new attribute
- How to override the main create method in odoo11?
- Redefining variable inside the loop python
- I am looking to define two library with different names that use the same code
- why using std::sqrt failed if I redefine the sqrt function?
- Can I redefine a c++ static const defined in a OpenCascade/OCCT header at runtime?
- How to restrict an any type inside a choice group in XML schema?
- How to redefine Scheme/Racket fundamental forms?
- Appending to / redefining a function?
- allow to redefine the type name or not?
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Popular Tags
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
You can use toLocaleDateString();
fa-IR is for Farsi-Iran, but all the ISO country codes can be found here
also you can set options as second argument, for example: