Giter Club home page Giter Club logo

nepdate's Introduction

LogoWhiteCropped

GitHub Repo Stars

Package

Package NuGet Stable Downloads
NepDate NepDate NepDate

Installation

Install NepDate with the package manager console

Install-Package NepDate

Install NepDate with the .NET CLI

dotnet add package NepDate

Features

NepDate is a super-fast and memory-efficient struct based on .NET Standard 2.0 that closely resembles the DateOnly struct in .NET with built in powerful features related to Nepali date functionality.

Initialization

By nepali year, month, and day values

using NepDate;

var nepDate = new NepaliDate(2079, 12, 16);

By Nepali date as a string

using NepDate;

var nepDate = new NepaliDate("2079/12/16");
// or
var nepDate = NepaliDate.Parse("2079/12/16");

By English DateTime

using NepDate;
using NepDate.Extensions;

var nepDate = new NepaliDate(DateTime.Now);
// or
var nepDate = DateTime.Now.ToNepaliDate();

By Nothing

using NepDate;

var nepDate = NepaliDate.Now; // Initializes NepaliDate object with current Nepali date

Properties

using NepDate;

var nepDate = new NepaliDate("2079/12/16");

// get Nepali year, month, and day values
nepDate.Year; // 2079
nepDate.Month; // 12
nepDate.Day; // 16

// get the equivalent English date as a DateTime
nepDate.EnglishDate; // 2023/03/30

// get the day of the week as a DayOfWeek enum value
nepDate.DayOfWeek; // Thursday

//Gets the day of the year, expressed as a value between 1 and 365
nepDate.DayOfYear; //245

// get the last day of the month as an integer value
nepDate.MonthEndDay; // 30

// get the last day of the month as a NepaliDate object
nepDate.MonthEndDate; // 2079/12/30

// get the name of the month as a NepaliMonths enum
nepDate.MonthName; // Chaitra

Formatting

using NepDate;

var nepDate = new NepaliDate("2079/02/06");

// Obtain the Nepali date represented as a string in its default format.
nepDate.ToString(); // 2079/02/06

// Customize the output by specifying different date formats, separators, and the use of leading zeros if needed.
nepDate.ToString(DateFormats.DayMonthYear, Separators.Dash, leadingZeros: false); // 6-2-2079

// Retrieve a representation of the date with options to omit leading zeros, display the day name, and exclude the year.
nepDate.ToLongDateString(leadingZeros: false, displayDayName: true, displayYear: false); // Friday, Jestha 6

// Obtain the date in Nepali numerical format, with options to specify the format, separators, and the use of leading zeros.
nepDate.ToUnicodeString(DateFormats.DayMonthYear, Separators.Dot, leadingZeros: true); // ०६.०२.२०७९

// Retrieve the Nepali representation of the date, with options to exclude leading zeros, display the day name, and exclude the year.
nepDate.ToLongDateUnicodeString(leadingZeros: false, displayDayName: true, displayYear: false); // शुक्रबार, जेठ ६

Add & Subtract Nepali Months & Days

using NepDate;

var nepDate = new NepaliDate("2081/04/32");

// Increment or decrement the Nepali date by a specified number of days.
nepDate.AddDays(05); // 2081/05/05
nepDate.AddDays(-5); // 2081/04/27

// Adjust the Nepali date by adding or subtracting a specified number of months.
nepDate.AddMonths(2); // 2081/06/30
nepDate.AddMonths(-2); // 2081/02/32

// Can also provide month as decimal
nepDate.AddMonths(2.5); // 2081/07/15

// When adding months, if the resulting month doesn't have the same end date as the input month,
// 'awayFromMonthEnd' option ensures adjustment away from the end of the resulting month.
nepDate.AddMonths(2, awayFromMonthEnd: true); // 2081/07/02

Fiscal Year

// Can obtain various fiscal year details using the NepaliDate Instance
var nepDate = new NepaliDate("2081/04/15");
nepDate.FiscalYearStartDate(); // 2081/04/01
nepDate.FiscalYearEndDate(); // 2082/03/31
nepDate.FiscalYearStartAndEndDate(); // (2081/04/01, 2082/03/31)
nepDate.FiscalYearQuarterStartDate(); // 2081/04/01
nepDate.FiscalYearQuarterEndDate(); // 2081/06/30
nepDate.FiscalYearQuarterStartAndEndDate(); // (2081/04/01, 2081/06/30)

// Also can achieve the same details through parameters
// Here, We take the first year as Fiscal Year. Eg: 2080 means Fy 2080/2081
NepaliDate.GetFiscalYearStartDate(2080); // 2080/04/01
NepaliDate.GetFiscalYearEndDate(2080); // 2081/03/31
NepaliDate.GetFiscalYearStartAndEndDate(2080); // (2080/04/01, 2081/03/31)
NepaliDate.GetFiscalYearQuarterStartDate(2080, 1); // 2081/01/01
NepaliDate.GetFiscalYearQuarterEndDate(2080, 1); // 2081/03/31
NepaliDate.GetFiscalYearQuarterStartAndEndDate(2080, 1); // (2081/01/01, 2081/03/31)

Bulk Conversion

var engDates = new List<DateTime>();
var nepDatesAsString = new List<string>();

// Converts a collection of English dates to Nepali dates
var newNepDates = NepaliDate.BulkConvert.ToNepaliDates(engDates);

// Converts a collection of Nepali date instances to English dates
var newEngDates = NepaliDate.BulkConvert.ToEnglishDates(newNepDates);

// Converts a collection of Nepali dates represented as strings to English dates
var newEngDates = NepaliDate.BulkConvert.ToEnglishDates(nepDatesAsString);

Additional Functions

using NepDate;

var nepDate = new NepaliDate("2079/12/16");

// determine if the Nepali year is a leap year
nepDate.IsLeapYear(); // False/True

nepDate.IsToday(); // False/True

nepDate.IsYesterday(); // False/True

nepDate.IsTomorrow(); // False/True

// subtract two Nepali dates to get a TimeSpan object
var nepDate2 = new NepaliDate("2080/12/16");
nepDate2 - nepDate; // Timespan object with value 365.00:00:00
// or
nepDate2.Subtract(nepDate); // Timespan object with value 365.00:00:00

// check if a string is a valid Nepali date and convert it to a NepaliDate object
if (NepaliDate.TryParse("2079/13/16", out var result))
{
    // use the result NepaliDate object
}

Comparing NepaliDate objects

using NepDate;

var nepDate = NepaliDate.Parse("2079/12/16");
var nepDate2 = DateTime.Now.ToNepaliDate();

if (nepDate == nepDate2)
{
    // the two NepaliDate objects are equal
}

if (nepDate < nepDate2)
{
    // nepDate is earlier than nepaliDate2
}

if (nepDate > nepDate2)
{
    // nepDate is after nepaliDate2
}

// etc

You just need the conversion?

using NepDate;

// Convert a DateTime directly to a NepaliDate string
var convertedToBS = DateTime.Now.ToNepaliDate().ToString();

// Convert a NepaliDate string directly to a DateTime
var convertedToAD = NepaliDate.Parse("2079/12/16").EnglishDate;

Parsing Nepali Date With AutoAdjust

// Parsing will try it's best to accurately identify the year, month and day
// And returns the date in the standard format of "yyyy/MM/dd"
// Below exmaples will demonstrate the probabilities.


// Replaces "_" To "/", Returns without adjusting if is already adjusted
var nepDate = NepaliDate.Parse("2077_05_25", autoAdjust: true); // 2077/05/25

// Replaces "-" To "/", Identifies '25' as day, '05' as month and '077' as year '2077'
var nepDate = NepaliDate.Parse("25-05-077", autoAdjust: true); // 2077/05/25

// Replaces "." To "/", Identifies '05' as month and '25' as day
var nepDate = NepaliDate.Parse("05.25.2077", autoAdjust: true); // 2077/05/25

// As '06' is on middle, Identifies it as month and '05' as day
var nepDate = NepaliDate.Parse("05/06/2077", autoAdjust: true); // 2077/06/05

// Identifies '05' as month due to parm 'monthInMiddle = false' and '06' as day
var nepDate = NepaliDate.Parse("05/06/2077", autoAdjust: true, monthInMiddle: false); // 2077/05/06

Performance

NepDate is distinguished by its capacity to perform with exceptional speed while utilizing minimal runtime memory resources. The metrics presented below exemplify NepDate's remarkable efficiency and proficiency, while remaining mindful of resource consumption.

The benchmarks can be found in NepDate.Benchmarks & NepDate.DotNetFrameworkBench

Output from latest run is

BenchmarkDotNet=v0.13.5, OS=Windows 11 (10.0.22621.1413/22H2/2022Update/SunValley2)
Intel Core i5-10400 CPU 2.90GHz, 1 CPU, 12 logical and 6 physical cores
.NET SDK=7.0.200
  [Host]     : .NET 7.0.3 (7.0.323.6910), X64 RyuJIT AVX2
  DefaultJob : .NET 7.0.3 (7.0.323.6910), X64 RyuJIT AVX2
Package Method Mean (ns) Error (ns) StdDev (ns) Rank Allocated (B)
NepDate BS -> AD 62.59 0.295 0.261 1️ -
NepDate AD -> BS 276.83 0.593 0.526 2️ 120
NepaliDateConverter.NETCORE BS -> AD 63,460.38 54.052 42.201 3️ 20176
NepaliDateConverter.NETCORE AD -> BS 186,610.23 420.217 350.901 7️ 20160
NepaliCalendarBS BS -> AD 99,511.43 247.038 231.080 5️ 159328
NepaliCalendarBS AD -> BS 113,258.50 364.280 340.748 6️ 158760
NepaliDateConverter.Net BS -> AD 75,327.75 269.244 251.851 4️ 20176
NepaliDateConverter.Net AD -> BS 212,478.96 4,192.698 5,877.576 8️ 20160

Change logs

https://github.com/TheCrossLegCoder/NepDate/releases

Contributions

Please view the CONTRIBUTING guide for more information.

nepdate's People

Contributors

samir-dahal avatar sanamhub avatar thecrosslegcoder avatar

Stargazers

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

Watchers

 avatar  avatar

nepdate's Issues

Date Mistake on 2062 Jestha Month

[
{
"DateVs": "2062/01/31",
"DateAd": "2005/05/14"
},
{
"DateVs": "2062/02/01",
"DateAd": "2005/05/15"
},
{
"DateVs": "2062/02/02",
"DateAd": "2005/05/16"
},
{
"DateVs": "2062/02/03",
"DateAd": "2005/05/17"
},
{
"DateVs": "2062/02/04",
"DateAd": "2005/05/18"
},
{
"DateVs": "2062/02/05",
"DateAd": "2005/05/19"
},
{
"DateVs": "2062/02/06",
"DateAd": "2005/05/20"
},
{
"DateVs": "2062/02/07",
"DateAd": "2005/05/21"
},
{
"DateVs": "2062/02/08",
"DateAd": "2005/05/22"
},
{
"DateVs": "2062/02/09",
"DateAd": "2005/05/23"
},
{
"DateVs": "2062/02/10",
"DateAd": "2005/05/24"
},
{
"DateVs": "2062/02/11",
"DateAd": "2005/05/25"
},
{
"DateVs": "2062/02/12",
"DateAd": "2005/05/26"
},
{
"DateVs": "2062/02/13",
"DateAd": "2005/05/27"
},
{
"DateVs": "2062/02/14",
"DateAd": "2005/05/28"
},
{
"DateVs": "2062/02/15",
"DateAd": "2005/05/29"
},
{
"DateVs": "2062/02/16",
"DateAd": "2005/05/30"
},
{
"DateVs": "2062/02/17",
"DateAd": "2005/05/31"
},
{
"DateVs": "2062/02/18",
"DateAd": "2005/06/01"
},
{
"DateVs": "2062/02/19",
"DateAd": "2005/06/02"
},
{
"DateVs": "2062/02/20",
"DateAd": "2005/06/03"
},
{
"DateVs": "2062/02/21",
"DateAd": "2005/06/04"
},
{
"DateVs": "2062/02/22",
"DateAd": "2005/06/05"
},
{
"DateVs": "2062/02/23",
"DateAd": "2005/06/06"
},
{
"DateVs": "2062/02/24",
"DateAd": "2005/06/07"
},
{
"DateVs": "2062/02/25",
"DateAd": "2005/06/08"
},
{
"DateVs": "2062/02/26",
"DateAd": "2005/06/09"
},
{
"DateVs": "2062/02/27",
"DateAd": "2005/06/10"
},
{
"DateVs": "2062/02/28",
"DateAd": "2005/06/11"
},
{
"DateVs": "2062/02/29",
"DateAd": "2005/06/12"
},
{
"DateVs": "2062/02/30",
"DateAd": "2005/06/13"
},
{
"DateVs": "2062/02/31",
"DateAd": "2005/06/14"
}
]. verify once in Hamro patro and other calendars

Invalid Date Exception while parsing

Parsing Issue/Bug
While parsing date string ("2075/04/32") to NepaliDate, exception occurs : "Invalid nepali date format"

To Reproduce
Run this code:
var parsedDate = NepaliDate.Parse("2075/04/32")

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.