Conversion of time stamps

Source: Internet
Author: User

When we develop a time stamp that is often converted into time, we encounter two types of timestamps.

1.2233445673

2.3456677890498

There is no difference between the two, 13 is the second three digits is the precision value

We can get rid of it when we calculate.

1, calculate the current time

NSDate *nowdate = [nsdate date];

Get to Current time:

nowdate=====2015-07-18 03:16:50 +0000

2, turn the current time into " Yyyy-mm-dd "

nsdateformatter *etimeformate = [[nsdateformatter alloc]init];

[Etimeformate Setdateformat:@ "YYYY-MM-DD"];

nsstring *etimestr = [etimeformate stringfromdate: [nsdate date]];

3. Time stamp conversion to date

Stime = @ "1234567890123";

nstimeinterval Time = ([strtime doublevalue]/);

nsdate *date= [nsdate dateWithTimeIntervalSince1970: time];

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

[Formatter Setdateformat:@ "YYYY-MM-DD"];

nsstring *currdate = [Formatter stringfromdate:d ate];

Convert the same time stamp into time

nsdateformatter *twoformat = [[nsdateformatter alloc]init];

[Twoformat Setdateformat:@ "HH:mm:ss"];

nsstring *timedate = [Twoformat stringfromdate:d ate];

4. Calculate dates from a few days ago

change the current time format

nsinteger dis = ;//How many days ago

nstimeinterval oneday = $ * 1;//Time of day

nsdate *nowdate = [nsdate date];

nsdate *stimedate = [nowdate initwithtimeintervalsincenow:-oneday * dis];//with current time conversion

nsdateformatter *stimeformatter = [[nsdateformatter alloc]init];//conversion

[Stimeformatter Setdateformat:@ "YYYY-MM-DD"];

nsstring *stimestr = [Stimeformatter stringfromdate: stimedate]

Summarize:

What we often use is NSDateFormatter stringfromdate.

There are several ways to NSDate

-(instancetype) Initwithtimeintervalsincenow: (nstimeinterval) secs;//from now

-(instancetype) initWithTimeIntervalSince1970: (nstimeinterval) secs;//1970 year start

-(instancetype) Initwithtimeinterval: (nstimeinterval) secstobeadded sincedate: (nsdate *) date;

Conversion of time stamps

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.