1. Get timestamp
  • Unit: second, reserved six significant digits in the following format:1574068247.545103
NSDate *datenow = [NSDate date];
NSString *timeSp = [NSString stringWithFormat:@"%f", (double)[datenow timeIntervalSince1970]];
Copy the code
  • Unit: second integer in the following format:1574068265
NSDate *datenow = [NSDate date];
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
Copy the code
  • Unit: millisecond, integer, imprecise, followed by three0, the format is as follows:1574068602000
NSDate *datenow = [NSDate date];
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]*1000];
Copy the code
  • Unit: millisecond, integer, exact, format:1574070082387
/ / get the current time 0 seconds time NSDate * date = [NSDate dateWithTimeIntervalSinceNow: 0]; // * NSTimeInterval Time = [Date timeIntervalSince1970]*1000; NSString *timeStr = [NSString stringWithFormat:@"%.0f", time];
Copy the code
  1. Timestamp to date
// The timestamp passed in is /1000 NSTimeInterval timeInterval = [timeStr doubleValue]/1000; NSDate *detailDate = [NSDate dateWithTimeIntervalSince1970:timeInterval]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // Instantiate an NSDateFormatter object and set the time format. Here you can set the format as you wantsetDateFormat:@"yyyy-MM-dd HH:mm:ss SS"];
NSString *dateStr = [dateFormatter stringFromDate:detailDate];
Copy the code
  1. Comparison of two dates
/ / 1. To convert the two timestamp to date NSDate * date1 = [NSDate dateWithTimeIntervalSince1970:1451047216]. NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:1451847216]; NSDate *date3 = [date1 :date2]; // Start // if date1 is written earlier than date2 -- > Otherwise, NSDate *date3 = [date1 :date2]; NSLog(@"Earlier date: %@",date3); NSDate *date4 = [date1 laterDate:date2]; NSLog(@"Late date: %@",date4); BOOL type BOOL result = [date1 isEqualToDate:date2]; NSLog(@"%d",result);
Copy the code
  • Can solve cross-year, cross-month, flat leap year time processing problems
/ / 100 days NSDate * date = [NSDate dateWithTimeIntervalSinceNow: 24 * 60 * 60 * 100]. NSDate *nowDate = [NSDate date]; // The date is in ascending orderif ([nowDate compare:date] == NSOrderedAscending) {
    NSLog(@Now =2019-11-18, ndate=2020-02-26);
}
Copy the code
  1. The calendar componentNSCalendar
NSDate *nowDate = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; // Initialize the calendar component, You can select the component you want NSDateComponents *comps = [Calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitWeekdayOrdinal|N SCalendarUnitWeekOfMonth|NSCalendarUnitWeekOfYear|NSCalendarUnitYearForWeekOfYear fromDate:nowDate]; Return the index of the day of the week (1 = Sunday, 2 = Monday,... , 7 = Saturday) NSInteger weekDay = [comps weekDay]; NSInteger day = [comps day]; NSInteger weekOfYear = [comps weekOfYear];Copy the code
  1. commonNSDateFormatterThe following formats can be useddateFormatterSymbols are formatted separately to get the required data for processing
symbol instructions
y/yyy/yyyy/Y/YYY/YYYY/u/uu/uuu/uuuu/U/UUU/UUUU Full year
yy/YY/UU A 2-digit year
M/MM/L/LL What 1 ~ 12 months
MMM/LLL Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec in shorthand
MMMM/LLLL January/February/March/April/May/June/out/August/September/October/November/another awarding in full
d 1 to 31 (day of the month, with 0)
D 1 to 366 (day of a year, with 0)
e/c/cc 1 to 7 (Day of a week, Sunday is 1 with 0)
E~EEE/eee/ccc / Sun/Mon/Tue/Wed/Thu Fri/Sat shorthand (week)
EEEE/eeee/cccc Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday whole put together (week)
H The value ranging from 0 to 23 is in the 24-hour system
h The value ranging from 1 to 12 with 0 in it is in the 12-hour system
k 1 to 24 Indicates the number of hours in a day. If the value is 0, the system is 24 hours
K The value ranging from 0 to 11 is in the 12-hour system
m 0 ~ 59 minutes
s 0 ~ 59 seconds
SSS ms
a AM/PM (上午/下午)
A 0 to 86399999 (microseconds of a day)
F Week of a month 1-5
w 1 to 53 Week of a year. The week begins on Sunday and begins on the last Sunday of the year
W 1 to 5 Week in a month. The beginning of the week is Sunday
q/qq/Q/QQ 1 to 4 Quarters
qqq/QQQ Short for Q1/Q2/Q3/Q4
qqqq/QQQQ 1st Quarter /2nd Quarter /3rd quarter/4th quarter
z~zzz Specify the abbreviation of GMT+8
zzzz/vvvv Specify the name of the GMT Time zone, China Standard Time
Z~ZZZ Specifies the GMT time zone abbreviation, +0800
ZZZZ Specify the abbreviation of GMT+08:00
v/VVVV Specify the name of the GMT Time zone, China Mainland Time
VV Specify the name of the GMT time zone, Asia/Shanghai
VVV Specify the name of the GMT time zone, Shanghai

Attached: my blog address