package
0.0.0-20241113112105-d5176070b8d9
Repository: https://github.com/ortizdavid/go-nopain.git
Documentation: pkg.go.dev
# README
datetime
Package for handling and manipulating dates and times with ease. It provides utilities for date and time conversion, as well as performing operations such as adding or subtracting days, weeks, months, and years, calculating time differences, and formatting human-readable time ago strings.
Features
-
Date and Time Conversion:
- Convert between
time.Time
and string representations. - Support for
YYYY-MM-DD
andYYYY-MM-DD HH:MM:SS
formats.
- Convert between
-
Date Operations:
- Add or subtract days, weeks, months, or years from a given date.
- Calculate the difference between two dates.
-
Age Calculation:
- Calculate the age based on a given birthdate.
-
Time Ago Functionality:
- Return a human-readable string indicating how much time has passed since a given date or between two dates.
# Functions
AddDays adds the specified number of days to the given date.
AddDaysStr adds the specified number of days to the given date string and returns the resulting date as a formatted string.
AddMonths adds the specified number of months to the given date.
AddMonthsStr adds the specified number of months to the given date string and returns the resulting date as a formatted string.
AddWeeks adds the specified number of weeks to the given date.
AddWeeksStr adds the specified number of weeks to the given date string and returns the resulting date as a formatted string.
AddYears adds the specified number of years to the given date.
AddYearsStr adds the specified number of years to the given date string and returns the resulting date as a formatted string.
CurrentDate returns the current date as a formatted string in the format "2006-01-02".
CurrentDateTime returns the current date and time as a formatted string in the format "2006-01-02 15:04:05".
CurrentHour returns the current hour as a formatted string in the format "15".
CurrentTime returns the current time as a formatted string in the format "15:04:05".
CurrentYear returns the current year as a formatted string.
DateTimeToString converts a time.Time object to a string date and time in the format "2006-01-02 15:04:05".
DateToString converts a time.Time object to a string date in the format "2006-01-02".
ExtractDate returns the date in the format YYYY-MM-DD.
ExtractDateStr extracts the date (YYYY-MM-DD).
ExtractDateTime returns the date and time in the format YYYY-MM-DD HH:MM:SS.
ExtractDateTimeStr extracts the date and time (YYYY-MM-DD HH:MM:SS).
ExtractDateTimeZoneStr extracts the timezone from a date string.
ExtractDay returns the day of the month as a string in the format DD.
ExtractDayStr extracts the day.
ExtractHour returns the time in the format HH:MM:SS.
ExtractHourStr extracts the hour.
ExtractMilliseconds returns the time with milliseconds in the format HH:MM:SS.SSS.
ExtractMonth returns the month as a string in the format MM.
ExtractMonthStr extracts the month.
ExtractSeconds returns only the seconds part (SS) of the time.
ExtractSecondStr extracts the seconds.
ExtractTimeZone returns the time zone name and offset of the given time.
ExtractWeek returns the ISO week number as a string (e.g., "39" for week 39).
ExtractWeekStr extracts the ISO week.
ExtractYear returns the year as a string in the format YYYY.
ExtractYearStr extracts the year.
GetAge calculates the age based on the given birthdate.
GetAgeStr calculates the age based on the given birthdate in string format.
IsValidDate checks if a string is a valid date in the format "2006-01-02".
IsValidDateTime checks if a string is a valid date-time in the format "2006-01-02 15:04:05".
IsValidDay checks if a string is a valid day as part of a full date (YYYY-MM-DD).
IsValidHour checks if a string is a valid time in the format "15:04:05".
IsValidMillisecond checks if a string is a valid time with milliseconds (HH:MM:SS.SSS).
IsValidMonth checks if a string is a valid month as part of a full date (YYYY-MM).
IsValidSecond checks if a string is a valid time with seconds (SS).
IsValidYear checks if a string is a valid year.
LastDateOfYear returns the last date of the current year.
LastDateOfYearStr returns the last date of the current year as a formatted string.
LastDayOfCurrentMonth returns the last day of the current month.
LastDayOfCurrentMonthStr returns the last day of the current month as a formatted string.
LastDayOfCurrentWeek returns the last day (Sunday) of the current week.
LastDayOfCurrentWeekStr returns the last day (Sunday) of the current week as a formatted string.
LastDayOfMonth returns the last day of the specified month of a given year.
LastDayOfMonthStr returns the last day of the specified month and year as a formatted string.
LastDayOfWeek returns the last day (Sunday) of the specified week of a given year.
LastDayOfWeekStr returns the last day (Sunday) of the specified week and year as a formatted string.
StringToDate converts a string date in the format "2006-01-02" to a time.Time object.
StringToDateTime converts a string date and time in the format "2006-01-02 15:04:05" to a time.Time object.
SubtractDates calculates the difference between two dates and returns the result as a time.Duration.
SubtractDays subtracts the specified number of days from the given date.
SubtractDaysStr subtracts the specified number of days from the given date string and returns the resulting date as a formatted string.
SubtractMonths subtracts the specified number of months from the given date.
SubtractMonthsStr subtracts the specified number of months from the given date string and returns the resulting date as a formatted string.
SubtractWeeks subtracts the specified number of weeks from the given date.
SubtractWeeksStr subtracts the specified number of weeks from the given date string and returns the resulting date as a formatted string.
SubtractYears subtracts the specified number of years from the given date.
SubtractYearsStr subtracts the specified number of years from the given date string and returns the resulting date as a formatted string.
SumDates calculates the sum of two dates and returns the resulting date.
SumDatesStr calculates the sum of two dates in string format and returns the result as a formatted string.
TimeAgo calculates the time difference from a given time to the current time and returns a human-readable string.
TimeAgoBetweenDates calculates the time difference between two time values and returns a string representing the duration.
TimeAgoBetweenDatesStr calculates the time difference between two time strings and returns a string representing the duration.
TimeAgoStr calculates the time difference from a given time string to the current time and returns a human-readable string.