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.