Time stamp and time conversion __c language in iOS development

Source: Internet
Author: User
Tags set time

Because the time stamp and time conversion is not very common, so in the use of the time is only to find the code online. Today found that to find code on the Internet will still take a certain amount of time, so time-consuming to find the code as well as their own record of the code to use next time.

Code on:

#pragma mark---Convert Time to time stamp

-(NSString *) gettimestampfromtime{

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

[Formatter Setdatestyle:nsdateformattermediumstyle];

[Formatter Settimestyle:nsdateformattershortstyle];

[Formatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"]; ----------set the format you want, the difference between HH and HH: 12-hour system, 24-hour system, respectively.

Setting the time zone, which is sometimes important for the processing of times

For example, you publish information in the country, the user in another time zone abroad, you want to let users see the correct time to publish to pay attention to time zone settings, the conversion of times.

For example, when you publish the time for 2010-01-26 17:40:50, then the British Irish side of the time to see how much?

They have a 7-hour jet lag with us, so they're not at this time yet ... That's what we're going to do with the future.

nstimezone* TimeZone = [Nstimezone timezonewithname:@ "Asia/shanghai"];

[Formatter Settimezone:timezone];

NSDate *datenow = [NSDate date];//now time, you can output to see what format

NSString *nowtimestr = [Formatter stringfromdate:datenow];//----------convert NSDate to formatter in nsstring format

NSLog (@ "%@", nowtimestr);

Time-to-time stamp method:

NSString *timesp = [nsstring stringwithformat:@ '%ld ', (long) [Datenow timeIntervalSince1970]];

NSLog (@ "timesp:%@", TIMESP); Value of time stamp

return TIMESP;

}

#pragma mark----Convert time stamp to time

-(NSString *) gettimefromtimestamp{

Converts the time of an object type to the NSDate type

Double time = 1504667976;

NSDate * Mydate=[nsdate Datewithtimeintervalsince1970:time];

Set time format

NSDateFormatter * Formatter=[[nsdateformatter Alloc]init];

[Formatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];

Convert Time to String

NSString *timestr=[formatter Stringfromdate:mydate];

return timestr;

At the same time, I hope to help friends who also need help.


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.