Get time and get time interval under iOS

Source: Internet
Author: User
<span id="Label3"></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><span style="color:#703daa">----------------------------------</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><span style="color:#703daa">1, Gets the current time</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px"><p style="margin-top:0px; margin-bottom:0px; font-size:14px"><span style="color:rgb(112,61,170)"><span style="font-family:Menlo; color:rgb(0,132,0); font-size:14px">//</span> <span style="color:rgb(0,132,0); font-size:14px"> <span style="font-family:Heiti SC Light">Get system Current time</span></span></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px"><p style="margin-top:0px; margin-bottom:0px; font-size:14px"><span style="color:rgb (112,61,170)"><span style=" Color:rgb (0,132,0); font-size:14px "><span style=" font-family:heiti SC light "><span style=" white-space:pre "><span style=" font-family : Menlo; font-size:14px; Color:rgb (112,61,170) ">nsdate </span> <span style=" font-family:menlo; font-size:14px "><span style=" font-family: Menlo; font-size:14px ">*<span style=" font-family:menlo; font-size:14px ">currentdate </span> </span> = [</span> <span style="font-family:menlo; font-size:14px; Color:rgb (112,61,170) ">nsdate </span> <span style=" font-family:menlo; font-size:14px; color:rgb (61,29,129) ">date </span> <span style="font-family:menlo; font-size:14px">]; </span> </span> </span> </span> </span> </p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px"><p style="margin-top:0px; margin-bottom:0px; font-size:14px"><span style="font-family:Menlo; color:rgb(112,61,170)">NSDateFormatter</span> <span style="font-family:Menlo">*dateformatter=[[</span><span style="font-family:Menlo; color:rgb(112,61,170)">nsdateformatter </span> <span style="font-family:Menlo; color:rgb(61,29,129)">alloc</span><span style="font-family:Menlo">] </span> <span style="font-family:Menlo; color:rgb(61,29,129)">init</span><span style="font-family:Menlo">];</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo">[dateformatter <span style="color:#3d1d81">setdateformat</span>:<span style="color:#d12f1b">@ "YYYYMMdd"</span>];</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><span style="color:#703daa">nsstring</span> *currentstring=[dateformatter <span style="color:#3d1d81">stringfromdate</span>: currentdate];</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(209,47,27)"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(209,47,27)"><span style="color:rgb(0,0,0)"> </span> <span style="color:#3d1d81">NSLog</span><span style="color:#000000">(</span>@ "currentstring:----------->%@"<span style="color:#000000">, currentstring);</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(209,47,27)"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(209,47,27)"><span style="color:rgb(112,61,170); font-family:Menlo; font-size:14px">2, get the date after a certain interval of time</span><br></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(209,47,27)"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(209,47,27)"><span style="color:#000000"></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:‘Heiti SC Light‘; color:rgb(0,132,0)"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:‘Heiti SC Light‘; color:rgb(0,132,0)"><span style="font-family:Menlo">//</span> Date after a certain interval</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><span style="color:#703daa">nsdate </span>*date = [<span style="color:#703daa">nsdate</span><span style="color:#3d1d81">date</span>];</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo">Date = [date<span style="color:#3d1d81">datebyaddingtimeinterval</span>:-<span style="color:#272ad8">5</span>*<span style="color:#272ad8">3600</span>*<span style="color:#272ad8"></span>];</p></p><br><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(209,47,27)"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(209,47,27)"><span style="color:#000000"><span style="color:rgb(112,61,170); font-family:Menlo; font-size:14px">3, get two time intervals, calculate two time intervals</span></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(209,47,27)"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(209,47,27)"><span style="color:#000000"><span style="color:rgb(112,61,170); font-family:Menlo; font-size:14px"><span style="white-space:pre"></span></span></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:‘Heiti SC Light‘; color:rgb(0,132,0)"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:‘Heiti SC Light‘; color:rgb(0,132,0)">//create date format object <span style="font-family:Menlo"> <span style="white-space:pre"></span> <span style="white-space:pre"></span> </span></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><span style="color:#703daa">nsdateformatter</span> *dateformatter=[[<span style="color:#703daa">nsdateformatter </span> <span style="color:#3d1d81">alloc</span>] <span style="color:#3d1d81">init</span>];</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo">[dateformatter <span style="color:#3d1d81">setdateformat</span>:<span style="color:#d12f1b">@ "yyyy-mm-dd hh:mm"</span>];</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; min-height:16px"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; min-height:16px"></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(0,132,0)"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(0,132,0)"><span style="color:#000000"> </span>// <span style="font-family:‘Heiti SC Light‘">created two date objects</span></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><span style="color:#703daa">nsdate</span> *date1=[dateformatter <span style="color:#3d1d81">datefromstring</span>:<span style="color:#d12f1b">@ "2010-3-3-11:00am"</span>];</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><span style="color:#703daa">nsdate</span> *date2=[dateformatter <span style="color:#3d1d81">datefromstring</span>:<span style="color:#d12f1b">@ "2010-3-4"</span>];</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(0,132,0)"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(0,132,0)"><span style="color:#000000"> </span>//nsdate *date=[nsdate date];</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(0,132,0)"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(0,132,0)"><span style="color:#000000"> </span>//nsstring *curdate=[dateformatter stringfromdate:date];</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; min-height:16px"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; min-height:16px"></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:‘Heiti SC Light‘; color:rgb(0,132,0)"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:‘Heiti SC Light‘; color:rgb(0,132,0)"><span style="font-family:Menlo; color:rgb(0,0,0)"> </span> <span style="font-family:Menlo">//</span> Take two date objects at a time interval:</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(0,132,0)"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(0,132,0)"><span style="color:#000000"> </span>// <span style="font-family:‘Heiti SC Light‘">The nstimeinterval here</span> is <span style="font-family:‘Heiti SC Light‘">not an object, is a basic type, in fact a</span> double <span style="font-family:‘Heiti SC Light‘">type, is</span> <span style="font-family:‘Heiti SC Light‘">defined</span> by C: typedef double nstimeinterval;</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><span style="color:#703daa">nstimeinterval</span> time=[date2 <span style="color:#3d1d81">timeintervalsincedate</span>:d ate1];</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; min-height:16px"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; min-height:16px"></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><span style="color:#bb2ca2">int</span> days= ((<span style="color:#bb2ca2">int</span>) time)/(<span style="color:#272ad8">3600</span>*<span style="color:#272ad8"></span>);</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><span style="color:#bb2ca2">int</span> hours= ((<span style="color:#bb2ca2">int</span>) time)% (<span style="color:#272ad8">3600</span>*<span style="color:#272ad8"></span>)/<span style="color:#272ad8">3600</span>;</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo"><span style="color:#703daa">nsstring</span> *datecontent=[[<span style="color:#703daa">nsstring </span> <span style="color:#3d1d81">alloc</span>] <span style="color:#3d1d81">initwithformat</span>:<span style="color:#d12f1b">@ "%i</span> <span style="font-family:‘Heiti SC Light‘; color:rgb(209,47,27)">Day </span> <span style="color:#d12f1b">%i</span> <span style="font-family:‘Heiti SC Light‘; color:rgb(209,47,27)">hours</span> <span style="color:#d12f1b">"</span>, days,hours];</p></p><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(209,47,27)"><p style="margin-top:0px; margin-bottom:0px; font-size:14px; font-family:Menlo; color:rgb(209,47,27)"><span style="color:#000000"><span style="color:rgb(112,61,170); font-family:Menlo; font-size:14px"><span style="white-space:pre"></span></span></span></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Get time and get time interval under iOS</p></p></span>

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.