In iOS development, we often have to deal with the time taken from the server. Similar to the Friends Circle, Weibo these apps. We can often see the words "just", "published 31 minutes Ago", "5 points Yesterday", and the like.
What we get from the server is the friend Circle information, or the time when the Weibo was created. So every time we display the data, we need to compare it to the current time, according to the format, figure out
The results we want.
For example, the time we get from the server is probably similar to the Sun may 12:12:00 +0800
Let's start with a detailed description of the date format .
- Years
- Y Displays the year (0-9) as a number without a leading zero
- YY Displays the year in a two-digit number format with leading zeros
- YYY Displays the year in four-digit number format
- YYYY displays the year in four-digit number format
- Month
- M Displays the month as a number without leading zeros (for example, January is represented as 1)
- MM Displays the month as a number with leading zeros (for example, 01/12/01)
- MMM Displays the month as an abbreviated form (e.g., Jan)
- MMMM display month as full month name (e.g. January)
- January Jan January
- February Feb February
- March Mar March
- April APR April
- May May
- June June June
- July Jul July
- August August
- September SEP September
- October OCT October
- November Nov November
- December Dec December
- Day
- D Displays the day as a number without leading zeros (for example, 1)
- DD Displays the day as a number with leading zeros, such as
- Week
- EEE Displays the day as an abbreviated form (e.g. Sun)
- Eeee display Day as full name (e.g. Sunday)
- Monday Mon Monday
- Tuesday Tue Tuesday
- Wednesday Wed Wednesday
- Thursday Thu Thursday
- Friday Fri Friday
- Saturday Sat Saturday
- Sunday Sun Sunday
- Hours
- H Displays the hour as a number without leading zeros using a 12-hour system (for example, 1:15:15 PM)
- HH Displays the hour as a number with leading zeros using a 12-hour system (for example, 01:15:15 PM)
- H Displays the hour as a number without leading zeros using a 24-hour system (for example, 1:15:15)
- HH Displays the hour as a number with leading zeros using a 24-hour system (for example, 01:15:15)
- Minutes
- M displays the minute as a number without leading zeros (for example, 12:1:15)
- MM Displays the minute as a number with leading zeros (for example, 12:01:15)
- Seconds
- S displays seconds as a number without leading zeros (for example, 12:15:5)
- SS displays seconds as a number with leading zeros (for example, 12:15:05)
- F shows the decimal part of the second
- FF will be displayed exactly to 1% seconds
- FFFF will display exactly one out of 10,000 seconds
- Up to seven F symbols can be used in a user-defined format
- Morning & Afternoon
- T use 12-hour system
- Show uppercase A for any hour before noon
- Any hour between noon and 11:59 to show the uppercase P
- TT for locales using the 12-hour format
- Show uppercase AM at any hour before noon
- Noon to 11:59 pm to show uppercase PM in any hour
- No characters are displayed for locales that use the 24-hour format
- Time
- Z shows the time zone offset without leading zeros
- ZZ Displays the time zone offset with leading zeros (for example, -08)
- ZZZ Displays the full time zone offset (for example, -0800)
- Era
- GG Displays the era/era string (for example, A.D.)
After understanding the above knowledge, we can write the code. Make a request beforehand.
Just ( within a minute )
X minutes ago ( within one hour )
X hours ago ( day )
yesterday hh:mm ( yesterday )
MM-DD ( year )
Yyyy-mm-dd hh:mm ( earlier )
Step One: We classify a date nsdate and then write a class method that converts the time data from the server to NSDate (the code continues to practice with Swift)
Let's follow the example of " Sun may 12:12:00 +0800"
1 ///make date string Create NSDate2 classFunc createdate (fulldatestring:string)->nsdate?{3 4 //Create Fomater5Let ft =NSDateFormatter ()6 //Setting up localization7 //2. To specify the date of the region, Xcode 6.3 Beta is not required, the region needs to specify English8 //Note: When debugging a real machine, be sure to specify the zone, otherwise the previous version cannot be converted9Ft.locale = Nslocale (localeidentifier:"en")Ten One //Set Date format AFt.dateformat ="EEE MMM dd HH:mm:ss zzz yyyy" - - //Build Date the returnft.datefromstring (fulldatestring) -}
The second step: Create Object Methods (functions) in the NSDate classification, formal processing time, the use of the Nscalendar Class (calendar)
Func fulldescription ()string{//get the current date with the Calendar classLet calendar =Nscalendar.currentcalendar ()//Create DatefomaterLet ft =NSDateFormatter ()//Day ifCalendar.isdateintoday (self) {//gets the difference between the date and the current timeLet delta =Int (NSDate (). Timeintervalsincedate (self))ifDelta < - { return "just" } ifDelta <3600{ return "\ (delta/60) minutes ago" } return "\ (delta/3600) hours ago" } //Yesterday ifCalendar.isdateinyesterday (self) {Ft.dateformat="Yesterday hh:mm" returnft.stringfromdate (self)}//Calculate the Year differenceLet coms = calendar.components (Nscalendarunit.calendarunityear, Fromdate:nsdate (), Todate:self, Options: Nscalendaroptions (0)) //this year ifComs.year = =0{Ft.dateformat="MM-DD" returnft.stringfromdate (self)}//All that's left is in the past.Ft.dateformat ="YYYY-MM-DD hh:mm" returnft.stringfromdate (self)}
Step three: After the above two methods are written, each time you call to get the data you want
Desired data = nsdate.createdate ("Server Record Creation Time"). Fulldescription ()
iOS Development-date processing (like the sending time of a friend circle, Weibo, etc.)