iOS Time and date processing

Source: Internet
Author: User
Tags date1 dateformat time zones time and date

NSDate: Date-related classes

Nstimeinterval: Time interval-related classes

NSDateFormatter: Time Formatter-related classes

Learn the knowledge points of the SectionTo:1. Date to timestamp

2. Date Turn string

3. Date to the specified format of the string (to obtain the day of the week, to obtain the date format display, etc.)

4. Date of time stamp transfer

5. String Turn Date

6. Specifying the format string to go to date

7. Calculation of Time

#pragma mark ======nsdate: Date-related class GTM (International Standard Time) ==========

-(void) viewdidload{

[Super Viewdidload];

initialization, same as normal data initialization (get International Standard Time, 8 time zones between our servers)

NSDate *date = [NSDate Date];

NSLog (@ "%@", date);

The printing results are as follows:

2016-01-06 14:34:24.964 time and date processing [3775:444683] 2016-01-06 06:34:24 +0000

Another way of initializing

/*

Nstimeinterval: Time interval unit is seconds

*/

This line of code means how many seconds from now on, past time (-) Future time (+)

Gets the current date and time

Nstimeinterval interval = 60*60*8;

NSDate *date1 = [nsdate datewithtimeintervalsincenow: (interval)];

NSLog (@ "%@", date1);

Get the date and time 10 days ago

Nstimeinterval interval1 = interval-60*60*24*10;

NSDate *date2 = [NSDate datewithtimeintervalsincenow:interval1];

NSLog (@ "%@", date2);

#pragma mark======= timestamp ============

Timestamp: is a time mark, a time interval (or string) from 1970 to the present

It can represent a unique time identifier

Calculated starting from 1970

NSDate *date3 = [NSDate datewithtimeintervalsince1970:1452044054];//Gets the date indicated by a timestamp

NSLog (@ "%@", date3);

How to convert #pragma mark===== date to time interval =======

The date is converted to a time interval of-->1. Time stamp can be obtained (refers to from 1970--now)

2. You can get a time interval between two dates

1??. Get a time interval between two dates

-(Nstimeinterval) Timeintervalsincedate: (NSDate *) anotherdate

1452044054 This timestamp corresponds to the time interval between the date and the current time

Nstimeinterval timeinterval =fabs ([date3 timeintervalsincedate:[nsdate Date]]);

NSLog (@ "%f", timeinterval);

Calculate how many hours apart from two time intervals how many seconds

int h = timeinterval/(60*60);

NSLog (@ "Two days of%d hours", h);

int s = ((int) timeinterval)% (60*60)/60;

NSLog (@ "%d minutes apart", s);

int m = ((int) timeinterval)% (60*60)%60;

NSLog (@ "%d seconds apart", M);

/*

method to take absolute value: do not differentiate positive and negative numbers (unsigned)

ABS (int)

Fabs (Double)

FABSF (float)

*/

#pragma mark======= date to timestamp ===========

2??. Time interval from now to 1970

Date Turn timestamp

NSDate *curdate = [NSDate Date];

NSLog (@ "%f", curdate.timeintervalsince1970);

NSString *string = [NSString stringwithformat:@ "%d", (int) curdate.timeintervalsince1970];

NSLog (@ "%@", string);

#pragma mark========== A comparison of two dates ===========

Compare 1451047216 and 1451847216

1. Convert these two timestamps to a date

NSDate *date4 = [NSDate datewithtimeintervalsince1970:1451047216];

NSDate *date5 = [NSDate datewithtimeintervalsince1970:1451847216];

2. Start comparing

Compare Date4 is not earlier than Date5 and will return a relatively early date

NSDate *date6 = [Date4 earlierdate:date5];

NSLog (@ "Older date%@", date6);

Compare two dates who is later than who

NSDate *date7 = [Date4 laterdate:date5];

NSLog (@ "%@", Date7);

Compare two dates is not the same--return value bool type

BOOL result = [Date4 isequaltodate:date5];

NSLog (@ "%d", result);

========= Date Formatter ==========

1?? Initialization

NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

property of the format *********

/* Y Year

Months in M-year

D the day of the year is the first number of days

Days in the D month

Number of weeks in the F month

E Week

A am/pm

H number of hours in a day (0-23)

K number of hours in a day (1-24)

Hours in K am/pm (0-11) number 0

Hours in H am/pm (1-12) Number 12

Minutes in M-hour number 30

Seconds in S minutes number 55

S MS Number 978

Z TimeZone general time zone Pacific PST; gmt-08:00

Z timezone RFC 822 time zone-0800

*/

Formatter.dateformat = @ "Yyyy-mm-dd hh:mm:ss E";

//    ?? The uppercase m represents the month, the lowercase m represents the points, and they represent different meanings.

//    ?? The uppercase HH indicates a 24-hour

//    ?? The lowercase HH indicates a 12-hour

//    ?? The uppercase SS represents milliseconds

//    ?? The lowercase SS represents the second

Convert dates to Strings

1. Must have a date

NSDate *now = [NSDate Date];

2. Convert using date Converters

NSString *datesting = [Formatter stringfromdate:now];

NSLog (@ "%@", datesting);

========== converts a string to a date ============

NSDate *returndate = [Formatter datefromstring:datesting];

NSLog (@ "%@", returndate);

=========== convert the timestamp to the desired format ========

Convert 1451847216 time stamps to a certain day of the week

Convert Timestamps to Dates

NSString *timestemp = @ "1451847216";

NSDate *date8 = [NSDate datewithtimeintervalsince1970:[timestemp doublevalue];

NSLog (@ "%@", Date8);

Initialize the date formatter and specify the format

NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

The format of the date format for a certain day of the week in a certain month

Formatter.dateformat = @ "yyyy mm month DD day E a";

Start conversion using the formatter

NSString *returnstring = [Formatter Stringfromdate:date8];

NSLog (@ "%@", returnstring);

============= Monday converted to Monday =============

}

/*

Job: On top of yesterday, add a field with the date of creation

Requirement: date is stored in timestamp form

Auto-Save to database when adding content

*/

/*

1. NSDate Initialization

NSDate *date = [NSDate date];//current time (International Standard Time)

NSDate *date1 = [NSDate datewithtimeintervalsincenow:interval];//get a time interval to the present date (used to calculate the previous or subsequent time) past time interval (-) Future time interval (+)

NSDate *date3 = [NSDate datewithtimeintervalsince1970:1452044015];//gets the date represented by a timestamp

2, NSDate, time stamp (1970-new), string representation

NSDate has a property of timeIntervalSince1970, which can be obtained to (1970-new) a time interval, to a string that represents the timestamp of the date

NSString *timestamp = [NSString stringwithformat:@ "%d", (int) curdate.timeintervalsince1970];

3. Comparison between two dates

-(NSDate *) Earlierdate: (nsdate *) anotherdate;//compare two dates who return early date

-(NSDate *) Laterdate: (nsdate *) anotherdate;//compare two dates who late return to late date

-(BOOL) Isequaltodate: (nsdate *) otherdate;//compare two dates return BOOL

*/

/*

NSDateFormatter convert dates into required formats

Format dates formatted with a string representation

@ "Yyyy-mm-dd HH:mm:ss"

@ "2016-01-06 11:06:30";

Role:

1. Convert Date to string (specify format)

2, string (specified format) converted to date

Will convert the GTM into system time.

1. Initialization

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

2. Format date format with string representation

Dateformatter.dateformat = @ "yyyy mm month DD day E a";

3. Using the Format Converter

(1) Turn the date into a string

NSString *dates = [Dateformatter stringfromdate:date20];

(2) Convert a string to a date

NSDate *date10 = [Formatter datefromstring:datestring];

*/

iOS time and date processing

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.