Some key uses of the IOS oc-date date classes

Source: Internet
Author: User
Tags current time date1
NSDate is a class of dates Create a date
*date gets the time in whichever time zone it is, the time of the corresponding 0 time zone is printed 
nsdate *date =[nsdate Date];
NSLog (@ "%@", date);
Results: 2015-07-23 16:04:00.431 oc07_nsdate[1430:100568] 2015-07-23 08:04:00 +0000
get the current time zone

Nstimezone *zone =[nstimezone Systemtimezone];
NSLog (@ "%@", zone);
Result: 2015-07-23 16:03:27.180 oc07_nsdate[1422:100218] Asia/shanghai (gmt+8) offset 28800 Get the number of seconds that differ from the 0 time zone

Nsinteger seconds =[zone Secondsfromgmtfordate:date];
NSLog (@ "%ld", seconds);
Results: 2015-07-23 15:59:40.371 oc07_nsdate[1406:99223] 28800 through the difference of seconds, can get to the present time

NSDate *localdate =[nsdate datewithtimeintervalsincenow:seconds];
NSLog (@ "%@", localdate);
nstimeinterval: Time interval

Nstimeinterval: Corresponding to the double type
Nstimeinterval calculates the time interval for two time objects

Nstimeinterval interval =[tomorrowdate timeintervalsincedate:date];
NSLog (@ "%g", interval);
Results: 2015-07-23 16:21:11.947 oc07_nsdate[1489:104070] 115200
Example 1
Calculate the current time and ⼀ a fixed time difference, if the difference in 60 seconds, output "just", if in 60 seconds outside 3,600 seconds, output "xx minutes ago," if 3,600 seconds, 3600*24 seconds, output "Xx⼩ hours ago."
    nsdate *date1=[nsdate datewithtimeintervalsincenow:1000];
Nstimeinterval time =[date1 timeintervalsincedate:date];
NSLog (@ "%ld", (Nsinteger) time);
if (time <60) {
    NSLog (@ "just");
} else if (time >=60) && (Time <3600) {
    NSLog (before%ld minutes), (Nsinteger) time/60);
else if (time >= 3600) && (time<3600*24)) {
    NSLog (@ "%g hours ago", time/3600);
}
convert dates and strings to each other
NSDate-> nsstring
    nsdate *date Date];
NSString *datestr =[nsstring stringwithformat:@ "%@", date];
NSLog (@ "%@", datestr);
Results: 2015-07-23 17:13:16.597 oc07_nsdate[1730:117413] 2015-07-23 09:13:16 +0000

String-> nsdate
And cut the time back 8 hours.

NSString *timestr =@ "2015-7-23 17-18-10";
NSDate *date=[formatter Datefromstring:timestr];
NSLog (@ "%@", date);

The format of the time
YYYY-MM-DD Hh-mm-ss
H 24-hour system, H 12-hour system first set the time format, to convert the time and format to match

NSDateFormatter *formatter =[[nsdateformatter alloc] init];
[Formatter setdateformat:@ "Yyyy-mm-dd Hh-mm-ss"];
NSDate *date=[nsdate Date];
Through the format, the specified time is directly converted to NSString in
 this way, the system will also switch time to the present time
nsstring *strdate =[formatter stringfromdate:date];
NSLog (@ "%@", strdate);
 Results: 2015-07-23 17:16:11.551 oc07_nsdate[1747:118395] 2015-07-23 17-16-11
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.