2.1.9 • Published 1 year ago
carbondate v2.1.9
carbondate
A simple DateTime Package based on dayjs. Also supports timezone. Developer friendly syntax.
Installation
npm i carbondateUses
import this in your project
import CarbonDate from "carbondate";Or
const CarbonDate = require('carbondate');Initialization
CarbonDate.init().now().format().value;
// 2024-05-27 13:09:59Or
const datetime = new CarbonDate();
datetime.now().format().value;
// 2024-05-27 13:09:59Or
CarbonDate.init().createFromObject({year: new Date().getFullYear(), month: (new Date().getMonth() + 1), day: new Date().getDate(), hour: '00', minute: '00', second: '00'}).toDateTimeString().value
// 2024-10-22 00:00:00Getters
CarbonDate.init().getYear().value
// 24Fluent Setters
CarbonDate.init().setYear(2001).setMonth(12).setDay(12).setHour(11).setMinute(45).setSecond(50).toDateTimeString().value
// 2001-12-12 11:45:50List of all available formats
CarbonDate.init().now().format('YYYY-MM-DD hh:mm:ss A').value
// 2024-05-27 02:06:21 PMParsing
CarbonDate.init().parse(new Date()).format().value
// 2024-05-27 13:10:11Or
CarbonDate.init().parse(CarbonDate.init().now().value).format().value
// 2024-05-27 13:10:20Or
CarbonDate.init().parse('2022').format().value
// 2022-01-01 00:00:00Or
CarbonDate.init().parse('2022-05').format().value
// 2022-05-01 00:00:00Or
CarbonDate.init().parse('2022-05-08').format().value
// 2022-05-08 00:00:00Or
CarbonDate.init().parse('10:55:10').format().value
// 2024-05-27 10:55:10Or
CarbonDate.init().parse('20:55:10').format('YYYY-MM-DD hh:mm:ss A').value
// 2024-05-27 08:55:10 PMOr
CarbonDate.init().parse('2023-05-04 20:55:10').format('YYYY-MM-DD hh:mm:ss A').value
// 2023-05-04 08:55:10 PMString Formatting
CarbonDate.init().now().toDateTimeString().value
// 2024-10-18 04:33:07Addition
Add One Day
CarbonDate.init().now().addDay().format().value
// 2024-05-28 13:19:59Add More than one Day
CarbonDate.init().now().addDays(2).format().value
// 2024-05-29 13:20:50List of all methods
Subtract
Subtract One Day
CarbonDate.init().now().subDay().format().value
// 2024-05-26 13:19:59Subtract More than one Day
CarbonDate.init().now().subDays(2).format().value
// 2024-05-25 13:20:50List of all methods
Difference
Difference in day between two days
CarbonDate.init().now().diffInDays(CarbonDate.init().now().subDay().value).value
// 1OR
CarbonDate.init().parse('2024-05-01').diffInDays(CarbonDate.init().parse('2024-05-05').value).value
// 4List of all methods
Comparison
Comparing two dates
CarbonDate.init().parse('2024-05-01').lessThan(CarbonDate.init().parse('2024-05-05').value).value
// trueOR
CarbonDate.init().now().lessThan(CarbonDate.init().parse('2024-05-05').value).value
// falseList of all methods
Constants
CarbonDate.init().SUNDAY
// 0OR
let instance = new CarbonDate();
instance.SUNDAY
// 0Common Functions
CarbonDate.init().yesterday().toDateTimeString().value
// 2024-10-21 04:30:05Leap Year
CarbonDate.init().yesterday().checkLeapYear().value
// trueDays in month
CarbonDate.init().now().dayCountInMonth().value
// as now is May so output is: 31Start of month
CarbonDate.init().now().startOfMonth().format('YYYY-MM-DD').value
// 2024-05-01End of month
CarbonDate.init().now().endOfMonth().format('YYYY-MM-DD').value
// 2024-05-31Diff for human
CarbonDate.init().now().diffForHumans().value
// a few seconds agoOr
CarbonDate.init().now().diffForHumans(CarbonDate.init().now().subHour().value).value
// in an hourMonth Number
- be careful with the month name spelling
ORCarbonDate.init().monthNumber('January').value // 01CarbonDate.init().monthNumber('jan').value // 01
dayOfYear
get date from day number
CarbonDate.init().now().dateFromDayNumber(300).format('YYYY-MM-DD HH:mm:ss').value // 2024-10-26 21:20:30OR
CarbonDate.init().parse('2024-09-05').dateFromDayNumber(300).format('YYYY-MM-DD HH:mm:ss').value // 2024-10-26 21:20:30get day number of year
CarbonDate.init().now().dayNumberOfYear().value // as now is 09-12-2024 output is: 344OR
CarbonDate.init().parse('2024-09-05').dayNumberOfYear().value // 249
Timezone
List of all available timezones.
Set your own Timezone
CarbonDate.init('America/Los_Angeles').now().format().value;
// 2024-05-28 06:12:26Or
const datetime = new CarbonDate('America/Los_Angeles');
datetime.now().format().value;
// 2024-05-28 06:12:26if you are not using any timezone. then it will take system timezone by default.
if you wish to use .env file for timezone; for central timezone for your app
- Install
dotenvpackage. usingnpm i dotenv - Create
.envfile to the root of your project - set timezone
APP_TIMEZONE='America/Los_Angeles'
After following this steps you can use like the example bellow:
CarbonDate.init().now().format().value;
// 2024-05-28 06:12:26 this is the time for America/Los_AngelesOr
const datetime = new CarbonDate();
datetime.now().format().value;
// 2024-05-28 06:12:26 this is the time for America/Los_AngelesCurrent Timezone
CarbonDate.init('America/Los_Angeles').currentTimezone().value
// America/Los_Angelesif you are not using .env file
CarbonDate.init().currentTimezone().value
// Asia/Dhaka my current timezoneConvert date time to a specific timezone
CarbonDate.init('America/Los_Angeles').parse('2024-05-10 10:00:50', true).format('YYYY-MM-DD hh:ss:mm A').value
// 2024-05-09 09:50:00 PMif you are using .env file
CarbonDate.init().parse('2024-05-10 10:00:50', true).format('YYYY-MM-DD hh:ss:mm A').value
// 2024-05-09 09:50:00 PM