Time and date in iOS

Source: Internet
Author: User
Tags date1 ming time and date

What do you say? Time and date are not great attributes, great features, but we must never be able to "snub" it.

One: How to "get the goods"--how to obtain time, date

//-=-== Current time------The "0 time zone" time is displayed by defaultNSDate * date1=[NSDate Date];//=-=-time Stamp//--1, the time is 60 seconds from 1970NSDate * date2=[nsdatedatewithtimeintervalsince1970: -];//--2,2000 YearNSDate * Date3=[nsdate datewithtimeintervalsincereferencedate: -* -];//-=--=-=---Now the time after "Nstimeinterval" startsNSDate * Date4=[nsdate Datewithtimeintervalsincenow: -* -];//-=-=-The date of "timeinterval" seconds after a date "Date1"NSDate * Date5=[nsdate datewithtimeinterval:60sincedate:date1];

Two: After getting the date and time, the format may be "Not what I want", what should I do?

Date formatting class: Nsformatter, you can get the format you want by this class, but be careful to format it on a certain basis:

Yyyy/yy: Year 2012/12

MM: Month

DD: "Japanese part"

HH: When

MM: Min

SS: Seconds

A:AM/PM (optional in 12-hour format)

ZZZ: Time zone

EEEE: Day of the week

The formatted string can also be converted back to the original nsdate type, but the string is formatted; (time default is 0 time zone, but after conversion is your current time zone)

/** Date format class: Nsformatter:nsobject*/NSDateFormatter* formatter=[[NSDate Formatter alloc] init];//-------= time converted to String =--------formatter.dateformat=@"yyyy lift mm head dd Hope hh Ming mm month ss a king zzz King Eeee"; NSString* datestr=[Formatter stringfromdate:date1]; NSLog (@"time turns into a string:%@", datestr);//-------= string converted to date-----=====/** The time at which the string is transferred is "time in the current time zone"*/NSString*string=@"2016 06 Head 28 hope 17 Ming 54 month gmt+8 King King Tuesday"; NSDate* Strdate=[formatter datefromstring:string]; NSLog (@"string Turn time:%@", strdate);

Three: Two dates of the morning and evening comparison

/** Compare Dates*/BOOL Flag=[Date1 Isequaltodate:date2];if(flag==NO) {NSLog (@"To think of my hometown"); }//--= Compare two dates sooner or later---=/** Return to an earlier time*/NSDate* date6=[Date1 Earlierdate:date2]; NSLog (@"%@", date6);/** Return to late time*/NSDate* date7=[Date1 Laterdate:date2]; NSLog (@"%@", Date7);}

Four: Date of control--uidatepicker

is to use Uidatepicker to select the time of the alarm;

How do I use it?

Uidatepicker * Picker=[[uidatepickeralloc] Initwithframe:cgrectmake (0, Kwidth, Kheight)];    Picker.backgroundcolor=[Uicolor cyancolor];    [Picker addtarget:selfaction: @selector (changeaction:) forcontrolevents:uicontroleventvaluechanged]; /*  */    picker.datepickermode=uidatepickermodedateandtime;    [Self.view Addsubview:picker];

It's so easy.

Uidatepicker:uicontrol How events can be added (AddTarget) to trigger events: uicontroleventvaluechanged

#pragma mark-value changed listener event-(void) Changeaction: (Uidatepicker *) P{nslog (@ "%@  ", P.date);}

Intended to catch the cicada, suddenly shut up.

Time and date in iOS

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.