Convert between IOS time and timestamp

Source: Internet
Author: User

Timestamp conversion with integer value in milliseconds
    • Time stamp converted to time NSDate
- (NSString *) Timewithtimeintervalstring: (NSString *) timestring{//Format time nsdateformatter* formatter = [[NSDateFormatter alloc] init]; formatter.timezone = [nstimezone Timezonewithname:@ "Shanghai"]; [Formatter Setdatestyle:Nsdateformattermediumstyle]; [Formatter Settimestyle:Nsdateformattershortstyle]; [Formatter Setdateformat:@ "yyyy mm month DD day hh:mm"]; //Millisecond value converted to seconds nsdate* date = [nsdate datewithtimeintervalsince1970:[timestring doublevalue]/ 1000.0]; c12>nsstring* datestring = [Formatter stringfromdate:date]; return datestring;}            
    • Time conversion to timestamp
        // 当前时间     NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];    NSTimeInterval a=[date timeIntervalSince1970]*1000; // *1000 是精确到毫秒,不乘就是精确到秒 NSString *timeString = [NSString stringWithFormat:@"%.0f", a]; //转为字符型 
    • Method of returning month and day by comparing time with current time
- (void) Getbabydetailage: (NSString *) date{Get Date ObjectNSDateFormatter *formatter_ = [[NSDateFormatter alloc] init]; Formatter_.dateformat =@ "Yyyy-mm-dd HH:mm:ss";nsdate *createdate = [Formatter_ datefromstring:date]; nscalendar *gregorian = [[nscalendar alloc] Initwithcalendaridentifier: nscalendaridentifiergregorian]; nsuinteger unitflags = nscalendarunitday | nscalendarunitmonth | nscalendarunityear; nsdatecomponents *components = [Gregorian components:unitflags fromDate:createDate Todate:[nsdate Date] Options: 0]; nsinteger years = [Components year]; nsinteger months = [Components month]; nsinteger days = [Components day];}         

Convert between IOS time and timestamp

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.