nepali
nepali
is a python package containing features that will be useful for Nepali projects.
The major feature of this package is nepalidatetime, which is compatible with python's datetime feature. It helps nepali date to english, parsing nepali datetime, nepali timezone, and timedelta support in nepali datetime.
Example
import datetime
from nepali import phone_number
from nepali.datetime import nepalidate, parser
nepali_datetime = parser.parse('2079-02-15')
date = datetime.date(2017, 3, 15)
nepali_date = nepalidate.from_date(date)
phone_number.parse("+977-9845217789")
Requirements
Python >= 3
Installation
pip install nepali
Features
- Date and Time
- Numbers
- Phone Number
- Locations
- For Django Template
Date and Time
date_converter
Date converter module converts english date to nepali and nepali date to english. It doesn't contain any extra functionality.
Convert English date to Nepali date
from nepali.date_converter import converter
np_year, np_month, np_date = converter.english_to_nepali(en_year, en_month, en_date)
Example
from nepali.date_converter import converter
np_year, np_month, np_date = converter.english_to_nepali(2023, 2, 7)
print(np_year, np_month, np_date)
Convert Nepali date to English date
from nepali.date_converter import converter
en_year, en_month, en_date = converter.nepali_to_english(np_year, np_month, np_date)
Example
from nepali.date_converter import converter
en_year, en_month, en_date = converter.nepali_to_english(2079, 10, 24)
print(en_year, en_month, en_date)
nepalidate
Creating a new nepalidate object
from nepali.datetime import nepalidate
np_date = nepalidate(year, month, day)
np_date = nepalidate.today()
np_date = nepalidate.strptime('2078-01-18', format='%Y-%m-%d')
Getting nepalidate object from python datetime
np_date = nepalidate.from_date(date_obj)
np_date = nepalidate.from_datetime(datetime_obj)
Attributes and Methods
np_date.year
np_date.month
np_date.day
np_date.to_date()
np_date.to_datetime()
np_date.to_nepalidatetime()
np_date.strftime("%Y-%m-%d")
np_date.strftime_ne("%Y-%m-%d")
np_date.weekday()
nepalidatetime
Creating a new nepalidatetime object
from nepali.datetime import nepalidatetime
np_datetime = nepalidatetime(year, month, day[, hour[, minute[, second]]])
np_datetime = nepalidate.now()
np_datetime = nepalidate.today()
np_datetime = nepalidatetime.strptime('2078-01-12 13:12', format='%Y-%m-%d %H:%M')
Getting nepalidatetime object from python datetime
np_datetime = nepalidatetime.from_date(date_obj)
np_datetime = nepalidatetime.from_datetime(datetime_obj)
Getting nepalidatetime object from nepalidate
np_datetime = nepalidatetime.from_nepalidate(nepali_date)
Attributes and Methods
np_date.year
np_date.month
np_date.day
np_date.hour
np_date.minute
np_date.second
np_date.to_date()
np_date.to_datetime()
np_date.to_nepalidate()
np_date.to_time()
np_date.strftime("%Y-%m-%d %H:%M")
np_date.strftime_ne("%Y-%m-%d %H:%M")
np_date.weekday()
Timedelta support
np_datetime - datetime.timedelta(days=3)
np_datetime1 - np_datetime2
np_datetime1 < np_datetime2
np_datetime1 >= datetime.datetime.now()
...
nepalihumanize
Returns readable form of nepali date.
from nepali.datetime import nepalihumanize
nepalihumanize(datetime, [threshold, format])
The threshold
is and optional field and is in seconds and the format is for the strftime
format. If the datetime object crosses the threshold it print the date with the format. The format
is also an optional and is %B %d, %Y
in default.
Example
from nepali.datetime import nepalihumanize, nepalidatetime
np_datetime = nepalidatetime(2079, 10, 5)
output = nepalihumanize(np_datetime)
output = nepalihumanize(np_datetime, threshold=1400)
timezone
NepaliTimeZone
You can use NepaliTimeZone
directly to your datetime object.
from nepali.timezone import NepaliTimeZone
datetime.datetime(2018, 8, 12, 16, 23, tzinfo=NepaliTimeZone())
now
Returns current datetime object with timezone
from nepali import timezone
timezone.now()
datetime.now()
vs timezone.now()
:
datetime.now()
doesn't contain timezone, but timezone.now()
will contain timezone of the system.
utc_now
Returns current UTC datetime object (with timezone UTC)
from nepali import timezone
timezone.utc_now()
parse
Parses date with commonly used date formats. Auto detects date format. If you are sure about the format, please use strptime
.
from nepali.datetime.parser import parse
np_datetime = parse(datetime_str)
Example
np_datetime = parse("2079-02-15")
np_datetime = parse("२०७८-०१-१८")
np_datetime = parse("2079/02/15")
np_datetime = parse("2079-02-15 15:23")
np_datetime = parse("2079-02-15 5:23 AM")
np_datetime = parse("2079-02-15 5:23 AM")
np_datetime = parse("Jestha 15, 2079")
strftime() and strptime() Format Codes
Directive | Meaning | Example |
---|
%a | Weekday as locale’s abbreviated name. | Sun, Mon, …, Sat (आइत, सोम, …) |
%A | Weekday as locale’s full name. | Sunday, Monday, …, Saturday |
%d | Day of the month as a zero-padded decimal number. | 01, 02, …, 31 |
%-d | Day of the month as a decimal number. | 1, 2, …, 31 |
%B | Month as locale’s full name. | Baishakh, Jestha, …, Chaitra |
%m | Month as a zero-padded decimal number. | 01, 02, …, 12 |
%-m | Month as a decimal number. | 1, 2, …, 12 |
%y | Year without century as a zero-padded decimal number. | 00, 01, …, 99 |
%Y | Year with century as a decimal number. | 2001, 2078, 2079, …, 2099 |
%H | Hour (24-hour clock) as a zero-padded decimal number. | 00, 01, …, 23 |
%-H | Hour (24-hour clock) as a decimal number. | 0, 1, 2, …, 23 |
%I | Hour (12-hour clock) as a zero-padded decimal number. | 01, 02, …, 12 |
%-I | Hour (12-hour clock) as a decimal number. | 1, 2, …, 12 |
%p | Locale’s equivalent of either AM or PM. | AM, PM (en_US) |
%M | Minute as a zero-padded decimal number. | 00, 01, …, 59 |
%-M | Minute as a decimal number. | 0, 1, 2, …, 59 |
%S | Second as a zero-padded decimal number. | 00, 01, …, 59 |
%-S | Second as a decimal number. | 0, 1, 2, …, 59 |
%f | Microsecond as a decimal number, zero-padded to 6 digits. | 000000, 000001, …, 999999 |
%% | A literal '%' character. | % |
Numbers
from nepali import number
convert
Converts english number to nepali.
np_number = number.convert("1234567890")
revert
Converts english number to nepali.
en_number = number.revert("१२३४५६७८९०")
add_comma
Adds comma in nepali numbers.
number_text = number.add_comma("1234567890")
nepalinumber
nepalinumber
is a new data type, which can be used to represent Nepali (Devanagari) numbers. It allows us to perform arithmetic operations, just like with int and float. Additionally, it can be used to parse numbers and output them in Devanagari format.
from nepali.number import nepalinumber
Parsing
a = nepalinumber("१८.२७")
print(a)
b = nepalinumber(15)
print(b)
Nepali (Devanagari) output
a = nepalinumber("18.27")
print(a.str_ne())
Arithmetic operations
a = nepalinumber("1")
b = nepalinumber("२")
c = a + b * 3
print(c)
Phone Number
from nepali import phone_number
is_valid
Checks is the given number is a valid nepali phone number.
phone_number.is_valid("9851377890")
phone_number.is_valid("+977-142314819")
phone_number.is_valid("8251377890")
parse
Parse phone number and returns details of the number.
phone_number.parse("9851377890")
phone_number.parse("+977-142314819")
Locations
Provides details of Nepal's Province, District, and Municipality.
from nepali.locations import provinces, districts, municipalities
from nepali.locations.utils import get_province, get_district, get_municipality
get_province(name="Bagmati")
get_district(name="Kathmandu")
get_municipality(name="Kathmandu")
get_municipality(name_nepali="विराटनगर")
For Django
We have created a new Django package called django-nepali to support nepali
package. For more information, please visit django-nepali.
Contribution
We appreciate feedback and contribution to this package. To get started please see our contribution guide