Conversion of time and time stamps in iOS

Source: Internet
Author: User

Gets the timestamp of the current system time

#pragma mark-Gets the timestamp of the current time

+ (Nsinteger) getnowtimestamp{

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, 24-hour system, respectively.

Setting the time zone, which is sometimes important

nstimezone* TimeZone = [Nstimezone timezonewithname:@ "asia/beijing"];

[Formatter Settimezone:timezone];

NSDate *datenow = [NSDate date];//now time

NSLog (@ "Device Current time:%@", [formatter stringfromdate:datenow]);

Time-to-timestamp method:

Nsinteger TIMESP = [[NSNumber numberwithdouble:[datenow timeIntervalSince1970]] integervalue];

NSLog (@ "Device current timestamp:%ld", (long) TIMESP); Value of time stamp

return TIMESP;

}

Convert a time to a timestamp

#pragma mark-Converts a time into a timestamp

+ (Nsinteger) Timeswitchtimestamp: (NSString *) formattime andformatter: (NSString *) format{

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

[Formatter Setdatestyle:nsdateformattermediumstyle];

[Formatter Settimestyle:nsdateformattershortstyle];

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

nstimezone* TimeZone = [Nstimezone timezonewithname:@ "asia/beijing"];

[Formatter Settimezone:timezone];

nsdate* date = [Formatter datefromstring:formattime]; ------------turn the string into NSDate by formatter

Time-to-timestamp method:

Nsinteger TIMESP = [[NSNumber numberwithdouble:[date timeIntervalSince1970]] integervalue];

NSLog (@ "Convert a time to timestamp &&&&&&&timesp:%ld", (long) TIMESP); Value of time stamp

return TIMESP;

}

Convert a timestamp to a time

#pragma mark-Converts a timestamp into time

+ (NSString *) Timestampswitchtime: (Nsinteger) timestamp andformatter: (NSString *) format{

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

[Formatter Setdatestyle:nsdateformattermediumstyle];

[Formatter Settimestyle:nsdateformattershortstyle];

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

Nstimezone *timezone = [Nstimezone timezonewithname:@ "asia/beijing"];

[Formatter Settimezone:timezone];

NSDate *CONFROMTIMESP = [NSDate datewithtimeintervalsince1970:timestamp];

NSLog (@ "1296035591 =%@", CONFROMTIMESP);

NSString *confromtimespstr = [Formatter stringfromdate:confromtimesp];

NSLog (@ "&&&&&&&CONFROMTIMESPSTR =:%@", CONFROMTIMESPSTR);

return confromtimespstr;

}

Conversion of time and time stamps in iOS

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.