2.1.9 • Published 7 months ago
carbondate v2.1.9
carbondate
A simple DateTime Package based on dayjs. Also supports timezone. Developer friendly syntax.
Installation
npm i carbondate
Uses
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:59
Or
const datetime = new CarbonDate();
datetime.now().format().value;
// 2024-05-27 13:09:59
Or
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:00
Getters
CarbonDate.init().getYear().value
// 24
Fluent Setters
CarbonDate.init().setYear(2001).setMonth(12).setDay(12).setHour(11).setMinute(45).setSecond(50).toDateTimeString().value
// 2001-12-12 11:45:50
List of all available formats
CarbonDate.init().now().format('YYYY-MM-DD hh:mm:ss A').value
// 2024-05-27 02:06:21 PM
Parsing
CarbonDate.init().parse(new Date()).format().value
// 2024-05-27 13:10:11
Or
CarbonDate.init().parse(CarbonDate.init().now().value).format().value
// 2024-05-27 13:10:20
Or
CarbonDate.init().parse('2022').format().value
// 2022-01-01 00:00:00
Or
CarbonDate.init().parse('2022-05').format().value
// 2022-05-01 00:00:00
Or
CarbonDate.init().parse('2022-05-08').format().value
// 2022-05-08 00:00:00
Or
CarbonDate.init().parse('10:55:10').format().value
// 2024-05-27 10:55:10
Or
CarbonDate.init().parse('20:55:10').format('YYYY-MM-DD hh:mm:ss A').value
// 2024-05-27 08:55:10 PM
Or
CarbonDate.init().parse('2023-05-04 20:55:10').format('YYYY-MM-DD hh:mm:ss A').value
// 2023-05-04 08:55:10 PM
String Formatting
CarbonDate.init().now().toDateTimeString().value
// 2024-10-18 04:33:07
Addition
Add One Day
CarbonDate.init().now().addDay().format().value
// 2024-05-28 13:19:59
Add More than one Day
CarbonDate.init().now().addDays(2).format().value
// 2024-05-29 13:20:50
List of all methods
Subtract
Subtract One Day
CarbonDate.init().now().subDay().format().value
// 2024-05-26 13:19:59
Subtract More than one Day
CarbonDate.init().now().subDays(2).format().value
// 2024-05-25 13:20:50
List of all methods
Difference
Difference in day between two days
CarbonDate.init().now().diffInDays(CarbonDate.init().now().subDay().value).value
// 1
OR
CarbonDate.init().parse('2024-05-01').diffInDays(CarbonDate.init().parse('2024-05-05').value).value
// 4
List of all methods
Comparison
Comparing two dates
CarbonDate.init().parse('2024-05-01').lessThan(CarbonDate.init().parse('2024-05-05').value).value
// true
OR
CarbonDate.init().now().lessThan(CarbonDate.init().parse('2024-05-05').value).value
// false
List of all methods
Constants
CarbonDate.init().SUNDAY
// 0
OR
let instance = new CarbonDate();
instance.SUNDAY
// 0
Common Functions
CarbonDate.init().yesterday().toDateTimeString().value
// 2024-10-21 04:30:05
Leap Year
CarbonDate.init().yesterday().checkLeapYear().value
// true
Days in month
CarbonDate.init().now().dayCountInMonth().value
// as now is May so output is: 31
Start of month
CarbonDate.init().now().startOfMonth().format('YYYY-MM-DD').value
// 2024-05-01
End of month
CarbonDate.init().now().endOfMonth().format('YYYY-MM-DD').value
// 2024-05-31
Diff for human
CarbonDate.init().now().diffForHumans().value
// a few seconds ago
Or
CarbonDate.init().now().diffForHumans(CarbonDate.init().now().subHour().value).value
// in an hour
Month Number
- be careful with the month name spelling
ORCarbonDate.init().monthNumber('January').value // 01
CarbonDate.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:30
OR
CarbonDate.init().parse('2024-09-05').dateFromDayNumber(300).format('YYYY-MM-DD HH:mm:ss').value // 2024-10-26 21:20:30
get day number of year
CarbonDate.init().now().dayNumberOfYear().value // as now is 09-12-2024 output is: 344
OR
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:26
Or
const datetime = new CarbonDate('America/Los_Angeles');
datetime.now().format().value;
// 2024-05-28 06:12:26
if 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
dotenv
package. usingnpm i dotenv
- Create
.env
file 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_Angeles
Or
const datetime = new CarbonDate();
datetime.now().format().value;
// 2024-05-28 06:12:26 this is the time for America/Los_Angeles
Current Timezone
CarbonDate.init('America/Los_Angeles').currentTimezone().value
// America/Los_Angeles
if you are not using .env
file
CarbonDate.init().currentTimezone().value
// Asia/Dhaka my current timezone
Convert 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 PM
if 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