Transformation of several formats of time nsdate in iOS development

Source: Internet
Author: User
Tags time zones time interval

In development, you often encounter the conversion of time format when interacting with the background.

First, several common nsdate formats are listed:

1. 20150605234106 format

2.2015-06-05-15:41:06 +0000 format (normal output format)

3.1433498400 format (number of seconds from 1970 to present)

the first format is converted to the second format

First format of time

nsstring* string = @ "20150605234106";

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

[Inputformatter Setlocale:[[nslocale alloc] initwithlocaleidentifier:@ "en_US"];

(Set the format you want, the difference between HH and HH: 12-hour system, 24-hour system respectively)

[Inputformatter setdateformat:@ "YYYYMMDDHHMMSS"];

nsdate* inputdate = [Inputformatter datefromstring:string];

The output is the date of the second format

NSLog (@ "date =%@", inputdate);

The first format is converted into a third format

Get to the current time

NSDate * nowdate=[nsdate Date];

NSLog (@ "nowdate =%@", nowdate);

The date of the third format is output

NSString * datestring=[nsstring stringwithformat:@ "%.0f", [Nowdate timeIntervalSince1970]];

NSLog (@ "datestring =%@", datestring);

The third format is converted to a string that can be exported

Date of the third format

NSString * datestring= @ "1433487206";

NSDate *updateddate=[[nsdate alloc] initwithtimeintervalsince1970:[datestring Doublevalue]];

The output is a string that can be displayed (a nsdate+category is required here to implement)

NSString * Result=[updateddate timeintervaldescription];

NSLog (@ "result =%@", result); Timeintervaldescription is an optional method that can be exported to date display formats in different formats

-(NSString *) timeintervaldescription;//distance from the current time interval description

-(NSString *) minutedescription;/* accurate to minute date description * *

-(NSString *) formattedtime;

-(NSString *) formatteddatedescription;//Format Date Description


No need for nsdate+category to achieve simple transformations

Long long int date=1433488265;

NSDate * Nowdate=[nsdate Datewithtimeintervalsince1970:date];(not a few minutes before the hint)



Hint; the time taken is calculated in 0 time zones. The time zone needs to be converted when exporting.



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.