You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
6.8 KiB

//
// NSDate+YYAdd.h
// YYKit <https://github.com/ibireme/YYKit>
//
// Created by ibireme on 13/4/11.
// Copyright (c) 2015 ibireme.
//
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
/**
Provides extensions for `NSDate`.
*/
@interface NSDate (YYAdd)
#pragma mark - Component Properties
///=============================================================================
/// @name Component Properties
///=============================================================================
@property (nonatomic, readonly) NSInteger year; ///< Year component
@property (nonatomic, readonly) NSInteger month; ///< Month component (1~12)
@property (nonatomic, readonly) NSInteger day; ///< Day component (1~31)
@property (nonatomic, readonly) NSInteger hour; ///< Hour component (0~23)
@property (nonatomic, readonly) NSInteger minute; ///< Minute component (0~59)
@property (nonatomic, readonly) NSInteger second; ///< Second component (0~59)
@property (nonatomic, readonly) NSInteger nanosecond; ///< Nanosecond component
@property (nonatomic, readonly) NSInteger weekday; ///< Weekday component (1~7, first day is based on user setting)
@property (nonatomic, readonly) NSInteger weekdayOrdinal; ///< WeekdayOrdinal component
@property (nonatomic, readonly) NSInteger weekOfMonth; ///< WeekOfMonth component (1~5)
@property (nonatomic, readonly) NSInteger weekOfYear; ///< WeekOfYear component (1~53)
@property (nonatomic, readonly) NSInteger yearForWeekOfYear; ///< YearForWeekOfYear component
@property (nonatomic, readonly) NSInteger quarter; ///< Quarter component
@property (nonatomic, readonly) BOOL isLeapMonth; ///< whether the month is leap month
@property (nonatomic, readonly) BOOL isLeapYear; ///< whether the year is leap year
@property (nonatomic, readonly) BOOL isToday; ///< whether date is today (based on current locale)
@property (nonatomic, readonly) BOOL isYesterday; ///< whether date is yesterday (based on current locale)
#pragma mark - Date modify
///=============================================================================
/// @name Date modify
///=============================================================================
/**
Returns a date representing the receiver date shifted later by the provided number of years.
@param years Number of years to add.
@return Date modified by the number of desired years.
*/
- (nullable NSDate *)dateByAddingYears:(NSInteger)years;
/**
Returns a date representing the receiver date shifted later by the provided number of months.
@param months Number of months to add.
@return Date modified by the number of desired months.
*/
- (nullable NSDate *)dateByAddingMonths:(NSInteger)months;
/**
Returns a date representing the receiver date shifted later by the provided number of weeks.
@param weeks Number of weeks to add.
@return Date modified by the number of desired weeks.
*/
- (nullable NSDate *)dateByAddingWeeks:(NSInteger)weeks;
/**
Returns a date representing the receiver date shifted later by the provided number of days.
@param days Number of days to add.
@return Date modified by the number of desired days.
*/
- (nullable NSDate *)dateByAddingDays:(NSInteger)days;
/**
Returns a date representing the receiver date shifted later by the provided number of hours.
@param hours Number of hours to add.
@return Date modified by the number of desired hours.
*/
- (nullable NSDate *)dateByAddingHours:(NSInteger)hours;
/**
Returns a date representing the receiver date shifted later by the provided number of minutes.
@param minutes Number of minutes to add.
@return Date modified by the number of desired minutes.
*/
- (nullable NSDate *)dateByAddingMinutes:(NSInteger)minutes;
/**
Returns a date representing the receiver date shifted later by the provided number of seconds.
@param seconds Number of seconds to add.
@return Date modified by the number of desired seconds.
*/
- (nullable NSDate *)dateByAddingSeconds:(NSInteger)seconds;
#pragma mark - Date Format
///=============================================================================
/// @name Date Format
///=============================================================================
/**
Returns a formatted string representing this date.
see http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
for format description.
@param format String representing the desired date format.
e.g. @"yyyy-MM-dd HH:mm:ss"
@return NSString representing the formatted date string.
*/
- (nullable NSString *)stringWithFormat:(NSString *)format;
/**
Returns a formatted string representing this date.
see http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
for format description.
@param format String representing the desired date format.
e.g. @"yyyy-MM-dd HH:mm:ss"
@param timeZone Desired time zone.
@param locale Desired locale.
@return NSString representing the formatted date string.
*/
- (nullable NSString *)stringWithFormat:(NSString *)format
timeZone:(nullable NSTimeZone *)timeZone
locale:(nullable NSLocale *)locale;
/**
Returns a string representing this date in ISO8601 format.
e.g. "2010-07-09T16:13:30+12:00"
@return NSString representing the formatted date string in ISO8601.
*/
- (nullable NSString *)stringWithISOFormat;
/**
Returns a date parsed from given string interpreted using the format.
@param dateString The string to parse.
@param format The string's date format.
@return A date representation of string interpreted using the format.
If can not parse the string, returns nil.
*/
+ (nullable NSDate *)dateWithString:(NSString *)dateString format:(NSString *)format;
/**
Returns a date parsed from given string interpreted using the format.
@param dateString The string to parse.
@param format The string's date format.
@param timeZone The time zone, can be nil.
@param locale The locale, can be nil.
@return A date representation of string interpreted using the format.
If can not parse the string, returns nil.
*/
+ (nullable NSDate *)dateWithString:(NSString *)dateString
format:(NSString *)format
timeZone:(nullable NSTimeZone *)timeZone
locale:(nullable NSLocale *)locale;
/**
Returns a date parsed from given string interpreted using the ISO8601 format.
@param dateString The date string in ISO8601 format. e.g. "2010-07-09T16:13:30+12:00"
@return A date representation of string interpreted using the format.
If can not parse the string, returns nil.
*/
+ (nullable NSDate *)dateWithISOFormatString:(NSString *)dateString;
@end
NS_ASSUME_NONNULL_END