Get current time

Source: Internet
Author: User

    nsstring* date;//Commit Current time    nsdateformatter* formatter = [[NSDateFormatter alloc]init];    [Formatter setdateformat:@ "Yyyy-mm-dd hh:mm:ss"];    date = [Formatter stringfromdate:[nsdate date]];    NSLog (@ "date = =%@", date);

Nscalendar *calendar =[[Nscalendar alloc] Initwithcalendaridentifier:nsgregoriancalendar]; NSDate*Now ; Nsdatecomponents*comps =[[Nsdatecomponents alloc] init]; Nsinteger Unitflags= Nsyearcalendarunit | Nsmonthcalendarunit | Nsdaycalendarunit | Nsweekdaycalendarunit |Nshourcalendarunit| Nsminutecalendarunit |Nssecondcalendarunit;now=[NSDate date];comps=[Calendar components:unitflags fromdate:now];week=[comps Weekday]; Month=[comps month];d ay=[Comps Day];hour=[Comps Hour];min=[Comps Minute];sec=[comps second];//Week:1--Sunday2--Monday3--Tuesday4--Wednesday5--Thursday6--Friday7--Saturday A simple record of how to get the system time in iOS, and later there are other more detailed features to update. NSString*date; NSDateFormatter* Formatter =[[Nsdateformatteralloc]init]; [Formattersetdateformat:@"YYYY-MM-DD Hh:mm:ss"]; Date=[formatterstringfromdate:[nsdatedate]];d ate shown as .- One- on  A: A: Ato find a few days ago, you can use this method nsdate* Date =[[NSDate alloc] init]; Date= [Date datebyaddingtimeinterval:-5*3600* -This is the time it takes to get to the front minus 5 days. Very useful. It's really enjoyable to do iphone development. A column from diqun1314 NSDate stores the world Standard Time (UTC), and the output needs to be converted to local time based on the time zone Dates the NSDate class provides the ability to create a date, compare date, and calculate the interval between two date.        The Date object is immutable. If you want to create a Date object and represent the current date, you can alloc a NSDate object and invoke init initialization: NSDate*now =[[NSDate alloc] init]; or use the Date class method of NSDate to create a day object. If you need a date different from the current date, you can use the NSDate initwithtimeinterval ... Or datewithtimeinterval ...         method, you can also use a more complex calendar or Date components object. Create a NSDate object for a certain time interval: Nstimeinterval secondsperday= -* -* -; NSDate*tomorrow =[[NSDate alloc] initwithtimeintervalsincenow:secondsperday]; NSDate*yesterday = [[NSDate alloc] initwithtimeintervalsincenow:-Secondsperday];  [Tomorrow release];          [Yesterday release]; Generate the NSDate object in a way that increments the time interval: nstimeinterval secondsperday= -* -* -; NSDate*today =[[NSDate alloc] init]; NSDate*tomorrow, *yesterday; Tomorrow=[Today Datebyaddingtimeinterval:secondsperday]; Yesterday= [Today Datebyaddingtimeinterval:-Secondsperday];          [Today release]; If you want to compare NSDate objects, you can use Isequaltodate:, compare:, Laterdate:, and Earlierdate: methods. These methods are precisely compared, meaning that these methods are always accurately compared to the second level of the NSDate object. For example, you might compare two dates if the interval between them is considered equal within a minute. In this case, use the Timeintervalsincedate: method to compare two dates. The following code makes an example:if(Fabs ([Date2 timeintervalsincedate:date1]) < -) ... Nscalendar&The nsdatecomponents Calendar object encapsulates the calculation of the system date, including the beginning of the year, the total number of days, and the division.        You will use the Calendar object to convert the absolute date to the date components (including year, month, day, time, minute, second). Nscalendar defines different calendars, including the Buddhist calendar, Gregorian calendar, etc. (these are all related to the localization settings provided by the system).        Nscalendar is closely related to nsdatecomponents objects. You can get the current system user-set calendar by Nscalendar The Currentcalendar method of the object. Nscalendar*currentcalendar =[Nscalendar Currentcalendar]; Nscalendar*japanesecalendar =[[Nscalendar alloc] Initwithcalendaridentifier:nsjapanesecalendar]; Nscalendar*userscalendar =[[Nslocale Currentlocale] Objectforkey:nslocalecalendar];         Userscalendar and Currentcalendar objects are equal, although they are different objects. You can use the Nsdatecomponents object to represent a component of a Date object-for example, year, month, day, and hour. If you want to make a Nsdatecomponents object meaningful, you must associate it with a calendar object. The following code example creates a Nsdatecomponents object: Nsdatecomponents*components =[[Nsdatecomponents alloc] init]; [Components Setday:6]; [Components Setmonth:5]; [Components setyear:2004]; Nsinteger Weekday= [Components weekday];//Undefined (= = nsundefineddatecomponent)to resolve a Date object to the corresponding date components, you can use the Nscalendar components:fromdate: method. In addition to the date itself, you need to specify the Nsdatecomponents object to return the component. NSDate*today =[NSDate Date]; Nscalendar*gregorian =[[Nscalendar alloc] Initwithcalendaridentifier:nsgregoriancalendar]; Nsdatecomponents*weekdaycomponents = [Gregorian components: (Nsdaycalendarunit |nsweekdaycalendarunit) Fromdate:today]; Nsinteger Day=[Weekdaycomponents Day]; Nsinteger Weekday=[weekdaycomponents Weekday]; You can also create NSDate objects from Nsdatecomponents objects: Nsdatecomponents*components =[[Nsdatecomponents alloc] init]; [Components Setweekday:2];//Monday[Components setweekdayordinal:1];//The first Monday in the month[Components Setmonth:5];// May[Components Setyear: -]; Nscalendar*gregorian =[[Nscalendar alloc] Initwithcalendaridentifier:nsgregoriancalendar]; NSDate*date =[Gregorian datefromcomponents:components]; To ensure proper behavior, you must ensure that the components used are meaningful on the calendar. Specifies an "Out of bounds" calendar component, such as a-6 or February 30 date values in the Gregorian calendar produce undefined behavior. You can also create a NSDate object without a year, so that the operating system will automatically generate a year, but will not use its auto-generated year in subsequent code. Nsdatecomponents*components =[[Nsdatecomponents alloc] init]; [Components Setmonth: One]; [Components Setday:7]; Nscalendar*gregorian =[[Nscalendar alloc] Initwithcalendaridentifier:nsgregoriancalendar]; NSDate*birthday =[Gregorian datefromcomponents:components]; The following example shows how to displace from one calendar to another: Nsdatecomponents*comps =[[Nsdatecomponents alloc] init]; [Comps Setday:6]; [Comps Setmonth:5]; [Comps Setyear:2004]; Nscalendar*gregorian =[[Nscalendar alloc] Initwithcalendaridentifier:nsgregoriancalendar]; NSDate*date =[Gregorian Datefromcomponents:comps];  [Comps release];    [Gregorian release]; Nscalendar*hebrew =[[Nscalendar alloc] Initwithcalendaridentifier:nshebrewcalendar]; Nsuinteger Unitflags= Nsdaycalendarunit | Nsmonthcalendarunit |Nsyearcalendarunit; Nsdatecomponents*components =[Hebrew components:unitflags fromdate:date]; Nsinteger Day= [Components day];// theNsinteger month = [Components month];//9Nsinteger year = [each year];//5764

Get current time

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.