iOS time formatting, timestamp, date formatting, common format conversion

created at 11-15-2021 views: 7

1) Convert current time to string (date→string)

"Mon Dec 7 11:25:11 2020" → "2020-12-07 11:25:11"

-(NSString *)getNowDateFormatString{
     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     //The format can be customized;
     //DD is the nth day of the whole year, dd is the nth day of a month;
     //HH is a 24-hour time system, hh is a 12-hour time system;
     [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

     NSDate *nowDate = [NSDate date];
     NSString *timeString = [formatter stringFromDate:nowDate];

     return timeString;

2) Current time to timestamp (date→integer)

"Mon Dec 7 11:52:20 2020" → "1607313140000"

     NSDate *datenow = [NSDate date];
     NSInteger timeStamp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];
     //The default precision is Miao (10 digits), usually the precision defined by the server is milliseconds, which can be multiplied by 1000;
     NSInteger timeInteger = timeStamp * 1000;
     return timeInteger;

3) Timestamp to string (integer→string)

-(NSString *)formatTimeWithTimeStamp:(NSInteger)integer{
     //Take the 13-bit timestamp as an example, the default precision of ios is wonderful, so divide by 1000 and then convert;
     //If the integer passed in here is wrong, it will return the start year "1970";
     NSInteger target = integer / 1000;
     NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:target];

     //Format into target time format
     NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
     [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
     NSString *timeString = [formatter stringFromDate:date];

     return timeString;

4) Unified format string form (two different formats of time, unified style)
Example① "2019-07-26 10:32:18"
Example ② "2019.07.26 10:32:18"

-(NSString *)dateFormatWithString:(NSString *)string{
     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     //Incoming date format
     [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
     //Convert the incoming string date into date
     NSDate *date =[formatter dateFromString:string];

     //Target date format
     [formatter setDateFormat:@"YYYY.MM.dd HH:mm:ss"];
     NSString *timeString = [formatter stringFromDate:date];

     return timeString;

5) Days difference (compared with the current time, the difference in days)

-(NSInteger)daysOfNumberWithString:(NSString *)lastString{
     // Here you need to pay attention to the lastString time format of the incoming parameter, and it must be unified with the current time format before you can compare it.
     NSInteger days;
     NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
     //The format here should be set to uniform
     [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

     NSDate *lastDate = [formatter dateFromString:laststring];
     //Call the above time formatting method
     NSString *nowString = [self getNowDateFormatString];
     NSDate *nowDate = [formatter dateFromString:nowString];

     NSTimeInterval time = [nowDate timeIntervalSinceDate:lastDate];
     days = time / (60 * 60 *24);

     return days;

6) How many days, hours, minutes and seconds
For example, how long has passed since the news published by Moments

-(NSString *)dateTimeDifferenceWithStartTime:(NSString *)startTime endTime:(NSString *)endTime
    NSDateFormatter *date = [[NSDateFormatter alloc]init];
    [date setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];

    NSDate *startDate =[date dateFromString:startTime];
    NSDate *endDdate = [date dateFromString:endTime];

    NSCalendar *cal = [NSCalendar currentCalendar];
    unsigned int unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *dateComponents = [cal components:unitFlags fromDate:startDate toDate:endDdate options:0];

    // sky
    NSInteger day = [dateComponents day];
    // Hour
    NSInteger house = [dateComponents hour];
    // Minute
    NSInteger minute = [dateComponents minute];
    // Second
    NSInteger second = [dateComponents second];
    NSString *timeStr;
    if (day != 0) {
       timeStr = [NSString stringWithFormat:@"%zd days%zd hours%zd minutes%zd seconds",day,house,minute,second];
    else if (day==0 && house !=0) {
       timeStr = [NSString stringWithFormat:@"%zd hours%zd minutes%zd seconds",house,minute,second];
    else if (day==0 && house==0 && minute!=0) {
       timeStr = [NSString stringWithFormat:@"%zd分%zdsec",minute,second];
       timeStr = [NSString stringWithFormat:@"%zdsec",second];
    return timeStr;

created at:11-15-2021
edited at: 11-15-2021: