When we develop a time stamp that is often converted into time, we encounter two types of timestamps.
1.2233445673
2.3456677890498
There is no difference between the two, 13 is the second three digits is the precision value
We can get rid of it when we calculate.
1, calculate the current time
NSDate *nowdate = [nsdate date];
Get to Current time:
nowdate=====2015-07-18 03:16:50 +0000
2, turn the current time into " Yyyy-mm-dd "
nsdateformatter *etimeformate = [[nsdateformatter alloc]init];
[Etimeformate Setdateformat:@ "YYYY-MM-DD"];
nsstring *etimestr = [etimeformate stringfromdate: [nsdate date]];
3. Time stamp conversion to date
Stime = @ "1234567890123";
nstimeinterval Time = ([strtime doublevalue]/);
nsdate *date= [nsdate dateWithTimeIntervalSince1970: time];
nsdateformatter *formatter = [[nsdateformatter alloc]init];
[Formatter Setdateformat:@ "YYYY-MM-DD"];
nsstring *currdate = [Formatter stringfromdate:d ate];
Convert the same time stamp into time
nsdateformatter *twoformat = [[nsdateformatter alloc]init];
[Twoformat Setdateformat:@ "HH:mm:ss"];
nsstring *timedate = [Twoformat stringfromdate:d ate];
4. Calculate dates from a few days ago
change the current time format
nsinteger dis = ;//How many days ago
nstimeinterval oneday = $ * 1;//Time of day
nsdate *nowdate = [nsdate date];
nsdate *stimedate = [nowdate initwithtimeintervalsincenow:-oneday * dis];//with current time conversion
nsdateformatter *stimeformatter = [[nsdateformatter alloc]init];//conversion
[Stimeformatter Setdateformat:@ "YYYY-MM-DD"];
nsstring *stimestr = [Stimeformatter stringfromdate: stimedate]
Summarize:
What we often use is NSDateFormatter stringfromdate.
There are several ways to NSDate
-(instancetype) Initwithtimeintervalsincenow: (nstimeinterval) secs;//from now
-(instancetype) initWithTimeIntervalSince1970: (nstimeinterval) secs;//1970 year start
-(instancetype) Initwithtimeinterval: (nstimeinterval) secstobeadded sincedate: (nsdate *) date;
Conversion of time stamps