IOS "objective-c microblogging release date format"

Source: Internet
Author: User
Tags american time dateformat locale reserved string format time and date


The time and date conversion of this style, similar to the microblogging release time, has been sorted and summarized as follows, and has solved so many key questions:



(1) Localize the time date string returned by the server (the server may not return a time format for the Chinese region). such as return to Europe and America time format. )



(2) Custom time date format, returning a string that conforms to the custom format



(3) How to determine whether a NSDate object is this/today/yesterday (to provide a classification for nsdate, three judgments are provided in two versions of the judgement respectively.) All versions of iOS are available/available after iOS 8 version)






Related code:



(1) Processing string format parts





/**
* Category format
* (1) this year
* 1 today
* a. within one minute just
* b. 1 minute to 59 minutes xx minutes ago
* c. greater than or equal to 60 minutes xx hours ago
* 2 Yesterday Yesterday xx:xx (hour: minute)
* 3 other days of the year xx-xx
*
* (2) Non-year xxxx-xx-xx
*/
 
 
/**
* preTimerStr == Thu Oct 16 17:06:25 +0800 2014 (time and date string returned by the server)
* dateFormat == EEE MMM dd HH:mm:ss Z yyyy (original format in Europe and America)
* E: Day of the week (these attributes are a few of them)
* M: Month
* d: a few (the first few days of the month)
* H: 24-hour hour
* m: minute
* s: seconds
* Z: time zone offset
* y: year
*/
 
 
 
// Pass in a timeStr and return the string of the corresponding format
- (NSString *)setupCreateTime:(NSString *)timeStr {
 
    // NSDateFormatter: NSString and NSDate
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    
    /*
    // If we get the time format of other regions, we need to set the locale (we got the start time in Europe and America, so say en_US here)
    Fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    
    // Set the date format (declare the meaning of each number and word in the string)
    fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy"; // Because the _created_at we obtained directly is the European and American time (NSString type, can not be used directly), so we must first obtain us in the European and American format. Convert the string to NSDate, then set the format once to convert NSDate to a string format
    
    // The date the microblog was created (converts the _created_at string object to an NSDate object)
    NSDate *createDateUS = [fmt dateFromString:timeStr]; // At this point
    
    // Set the date format again (and then use this date format to process the time string)
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
     */
    
    
    // format
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    // Publish date object
    NSDate *createDate = [fmt dateFromString:timeStr];
    
    // processing time (compared with the current time)
    // Determine if this year is equal. => Get date year => Calendar, get date component
    / / Get the post release time and the current time difference
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    // Get two date differences
    NSDateComponents *cmp = [calendar components:NSCalendarUnitHour | NSCalendarUnitMinute fromDate:createDate toDate:[NSDate date] options:NSCalendarWrapComponents];
    
    
    If ([createDate isThisYear]) {
        If ([createDate isThisToday]) {
            
            // Get the date difference
            If (cmp.hour >= 1) {
                timeStr = [NSString stringWithFormat:@"%ld hours ago", cmp.hour];
            } else if (cmp.minute >= 2) {
                timeStr = [NSString stringWithFormat:@"%ld minutes ago", cmp.minute];
            } else { // just
                timeStr = @"just";
            }
            
        } else if ([createDate isThisYesterday]) { // Yesterday
            // Yesterday 21:10
            fmt.dateFormat = @"Yesterday HH:mm";
            timeStr = [fmt stringFromDate:createDate];
            
        } else { // Other days of the year
            fmt.dateFormat = @"MM-dd";
            timeStr = [fmt stringFromDate:createDate];
        }
    } else { // not this year
        fmt.dateFormat = @"yyyy-MM-dd";
        timeStr = [fmt stringFromDate:createDate];
    }
    
    Return timeStr;
 
}

(2) Nsdate+date part



//
// NSDate+Date.h
// WZYBaiSi
//
// Created by Wang Zhongyu on 16/8/7.
// Copyright © 2016 wzy. All rights reserved.
//
 
#import <Foundation/Foundation.h>
 
@interface NSDate (Date)
 
// Is this year?
- (BOOL)isThisYear;
 
// Whether today
- (BOOL)isThisToday;
 
// Yes yesterday
- (BOOL)isThisYesterday;
 
@end


 

//
// NSDate+Date.m
// WZYBaiSi
//
// Created by Wang Zhongyu on 16/8/7.
// Copyright © 2016 wzy. All rights reserved.
//
 
#import "NSDate+Date.h"
 
@implementation NSDate (Date)
 
// Determine if this is this year
- (BOOL)isThisYear
{
    
    // Calendar object (for easy comparison of the gap between two dates)
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    // The NSCalendarUnit enumeration represents which difference you want to get (here the year, month, day, hour, minute, and second, then the difference will show the information of these items)
    // NSCalendarUnit unit = NSCalendarUnitYear; // This is just right below, no need to declare an NSCalendarUnit object
    
    
    // Get the "year" element of a certain time
    NSDateComponents *dateCmps = [calendar components:NSCalendarUnitYear fromDate:self]; // The creation time passed in
    
    NSDateComponents *nowCmps = [calendar components:NSCalendarUnitYear fromDate:[NSDate date]]; //current time
    
    Return dateCmps.year == nowCmps.year;
}
 
// Determine if it is today
- (BOOL)isThisToday
{
    NSDate *nowDate = [NSDate date];
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd";
    
    / / First convert these two NSDate objects into NSString objects
    NSString *dateStr = [fmt stringFromDate:self];
    NSString *nowStr = [fmt stringFromDate:nowDate];
    
    // Compare the date strings without time, minute, and second. When the year, month, and day are exactly the same, it is naturally the same day, which is today.
    Return [dateStr isEqualToString:nowStr];
    
}
 
// Determine if it is yesterday
- (BOOL)isThisYesterday
{
    NSDate *nowDate = [NSDate date];
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd";
    
    / / First convert these two NSDate objects into NSString objects
    NSString *dateStr = [fmt stringFromDate:self];
    NSString *nowStr = [fmt stringFromDate:nowDate];
    
    // Then convert the NSString to the NSDate format without time, minute, and second according to the fmt format (the purpose is to remove the time, minute, and second, simply compare the year, month, and day)
    NSDate *date = [fmt dateFromString:dateStr];
    nowDate = [fmt dateFromString:nowStr];
    
    
    // Calendar object (for easy comparison of the gap between two dates)
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    
    NSDateComponents *cmps = [calendar components:unit fromDate:date toDate:nowDate options:0];
    
    // We only judge the day in the same case as year and month. Year is different, even if yesterday was the last day of last year, we also displayed in the format of "non-this year". Month is different, even if yesterday is the last day of last month, we also display according to the format of "other days of this year"
    Return cmps.year == 0 && cmps.month == 0 && cmps.day == 1;
}
 
 
 
 
// Also provide a simpler way to write, but only for iOS 8 and later
/*
The NS_AVAILABLE macro tells us that this method was introduced with Mac OS 10.9 and iOS 8.0 respectively.
 
- (BOOL)isDateInToday:(NSDate *)date NS_AVAILABLE(10_9, 8_0);
- (BOOL)isDateInYesterday:(NSDate *)date NS_AVAILABLE(10_9, 8_0);
- (BOOL) isDateInTomorrow: (NSDate *) date NS_AVAILABLE(10_9, 8_0);
- (BOOL)isDateInWeekend:(NSDate *)date NS_AVAILABLE(10_9, 8_0);
 */
 
/*
- (BOOL)isThisYear
{
    / / Get the current date object
    NSDate *curDate = [NSDate date];
    / / Get the calendar class
    NSCalendar *curCalendar = [NSCalendar currentCalendar];
    // Get your own date component (year, month, etc.)
    NSDateComponents *selfCmp = [curCalendar components:NSCalendarUnitYear fromDate:self];
    // Get the current time and date component (year, month, etc.)
    NSDateComponents *curCmp = [curCalendar components:NSCalendarUnitYear fromDate:curDate];
    Return curCmp.year == selfCmp.year;
}
// Determine if it is today
- (BOOL)isThisToday
{
    / / Get the calendar class
    NSCalendar *curCalendar = [NSCalendar currentCalendar];
    Return [curCalendar isDateInToday:self];
}
- (BOOL)isThisYesterday
{
    NSCalendar *curCalendar = [NSCalendar currentCalendar];
    Return [curCalendar isDateInYesterday:self];
}
*/
 
@end 








Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.