Tmall learn important operations related to the time of the Weibo project in iOS (9) Weibo model, judging the previous day, yesterday, this year, and so on. ios practice

Source: Internet
Author: User

Tmall learn important operations related to the time of the Weibo project in iOS (9) Weibo model, judging the previous day, yesterday, this year, and so on. ios practice

CAT/CAT sharing, must be excellent

For Original Articles, please reprint them. Reprinted Please note: Yan Nai-yu's blog
Address: http://blog.csdn.net/u013357243

I. Effect

-- Because I am a black apple, the system time jump time is not displayed, but the code is correct

II. Implementation Code
/** 1. 1 this year> today * 1 minute: Just * 1 minute ~ Within 59 minutes: xx minutes ago * greater than 60 Minutes: xx hours ago 2> yesterday * Yesterday xx: xx 3> other * xx-xx: xx 2. not this year 1> xxxx-xx: xx */-(NSString *) created_at {NSDateFormatter * fmt = [[NSDateFormatter alloc] init]; // If debugging is performed on a real machine, you need to set locale fmt to convert the time in Europe and America. locale = [[NSLocale alloc] initWithLocaleIdentifier: @ "en_US"]; // set the date format (declare the meaning of each number and word in the string) // E: day of the week // M: month // d: Number of the day of the month // H: hour in the 24-hour format // m: minute // s: S // y: Year fmt. dateFormat = @ "eee mmm dd HH: mm: ss Z yyyy"; // _ created_at = @ "Tue Sep 30 17:06:25 + 0600 2014 "; // Weibo creation date NSDate * createDate = [fmt dateFromString: _ created_at]; // The current time NSDate * now = [NSDate date]; // calendar Object (to compare the gap between two dates) NSCalendar * calendar = [NSCalendar currentCalendar ar]; // alendarunit enumeration indicates the differences to be obtained. NSCalendarUnit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | hour; // calculates the difference between two dates. NSDateComponents * cmps = [calendar components: unit fromDate: createDate toDate: now options: 0]; if ([createDate isThisYear]) {// this year if ([createDate isYesterday]) {// Yesterday fmt. dateFormat = @ "Yesterday HH: mm"; return [fmt stringFromDate: createDate];} else if ([createDate isToday]) {// Today if (cmps. hour> = 1) {return [NSString stringWithFormat: @ "% d hour ago", cmps. hour];} else if (cmps. minute> = 1) {return [NSString stringWithFormat: @ "% d Minutes Ago", cmps. minute];} else {return @ "just now" ;}} else {// other days of this year fmt. dateFormat = @ "MM-dd HH: mm"; return [fmt stringFromDate: createDate] ;}} else {// not this year's fmt. dateFormat = @ "yyyy-MM-dd HH: mm"; return [fmt stringFromDate: createDate] ;}}
Time classification for judgment

Time classification is used to determine whether it is today, whether it is tomorrow, and so on. In short, this is very powerful.

/// NSDate + Extension. m // black horse Weibo 2 // Created by apple on 14-10-18. // Copyright (c) 2014 heima. all rights reserved. // # import "NSDate + Extension. h "@ implementation NSDate (Extension)/*** determine whether a certain time is this year */-(BOOL) isThisYear {NSCalendar * calendar AR = [NSCalendar currentCalendar]; // obtain the NSDateComponents * dateCmps = [calendar components: Refreshing fromDate: self]; NSDateComponents * nowCmps = [calendar components: NSCalendarUnitYear fromDate: [NSDate date]; return dateCmps. year = nowCmps. year;}/*** determine whether a time is yesterday */-(BOOL) isYesterday {NSDate * now = [NSDate date]; // date = 10:05:28 --> 00:00:00 // now = 09:22:10 --> 00:00:00 NSDateFormatter * fmt = [[NSDateFormatter alloc] init]; fmt. dateFormat = @ "yyyy-MM-dd"; // 2014-04-30 NSString * dateStr = [fmt stringFromDate: self]; // 2014-10-18 NSString * nowStr = [fmt stringFromDate: now]; // 00:00:00 NSDate * date = [fmt dateFromString: dateStr]; // 2014-10-18 00:00:00 now = [fmt dateFromString: nowStr]; NSCalendar * calendar = [NSCalendar currentCalendar]; NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; NSDateComponents * cmps = [calendar components: unit fromDate: date toDate: now options: 0]; return cmps. year = 0 & cmps. month = 0 & cmps. day = 1;}/*** determine whether a time is today */-(BOOL) isToday {NSDate * now = [NSDate date]; NSDateFormatter * fmt = [[NSDateFormatter alloc] init]; fmt. dateFormat = @ "yyyy-MM-dd"; NSString * dateStr = [fmt stringFromDate: self]; NSString * nowStr = [fmt stringFromDate: now]; return [dateStr is1_tostring: nowStr] ;}@ end
Iii. Supplement

Weekly and monthly English

January 1, January-Jan.
Feb, February, February.
Mar.
Apr.
May, May
June Jun.
July-Jul.
Aug.
September, September, abbreviated as Sep.
October, October, abbreviated as Oct.
November-Nov.
December, December, Dec.

Monday: Mon. = Monday
Tuesday: Tue. = Tuesday
Wed. = Wednesday
Thu. = Thursday
Friday: Fri. = Friday
Saturday: Sat. = Saturday
Sunday: Sun. = Sunday

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.