#define KDEFAULT_DATE_TIME_FORMAT (@ "Yyyy-mm-dd HH:mm:ss")
Get current date, time
+ (NSDate *) getcurrentdate{
NSDate *now = [NSDate Date];
return now;
}
Convert a date to a string (date, time)
+ (NSString *) Getdatestringfromdate: (NSDate *) date{
Nsinteger location = 0;
NSString *timestr = @ "";
NSDateFormatter *formatter = [[[[NSDateFormatter alloc] init] autorelease];
[Formatter Setlocale:[[[nslocale alloc] initwithlocaleidentifier:@ "en_US"] autorelease];
[Formatter setdateformat:@ "hh:mm:a"];
NSString *AMPM = [[[Formatter Stringfromdate:date] componentsseparatedbystring:@ ":"] objectatindex:2];
TIMESTR = [Formatter stringfromdate:date];
Nsrange range = [timestr rangeofstring:[nsstring stringwithformat:@ ":%@", ampm]];
location = Range.location;
NSString *string = [Timestr substringtoindex:location];
TIMESTR = [NSString stringwithformat:@ "%@%@", ampm,string];
NSString *datestr = @ "";
NSDateFormatter *dformatter = [[[[NSDateFormatter alloc] init] autorelease];
[Dformatter Setlocale:[[[nslocale alloc] initwithlocaleidentifier:@ "en_US"] autorelease];
[Dformatter setdateformat:@ "Yyyy-mm-dd"];
DATESTR = [Dformatter stringfromdate:date];
NSLog (@ "%@", [NSString stringwithformat:@ "%@%@", Datestr,timestr]);
return [NSString stringwithformat:@ "%@%@", Datestr,timestr];
}
Calculate the gap between two dates and how many days have gone by.
+ (Nsinteger) Getdatetodatedays: (NSDate *) Date withsavedate: (NSDate *) savedate{
nscalendar* Chineseclendar = [[Nscalendar alloc] Initwithcalendaridentifier:nsgregoriancalendar];
Nsuinteger unitflags = Nshourcalendarunit | Nsminutecalendarunit |
Nssecondcalendarunit | Nsdaycalendarunit | Nsmonthcalendarunit | Nsyearcalendarunit;
Nsdatecomponents *cps = [Chineseclendar components:unitflags fromdate:date todate:savedate options:0];
Nsinteger diffday = [cps day];
[Chineseclendar release];
return diffday;
}
Example: nsdate *lastdate = [self getsavedate];//savedate saves by converting NSDate to NSString
currentdate = [NSDate Date];
Nsinteger day = [Datehelper getdatetodatedays:currentdate withsavedate:lastdate];
Date to String
+ (NSString *) nsdatetonsstring: (NSDate *) Date
{
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[Formatter Setdateformat:kdefault_date_time_format];
NSString *datestring = [Formatter stringfromdate:date];
[Formatter release];
return datestring;
}
String to date
+ (NSDate *) Nsstringtonsdate: (NSString *) string
{
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[Formatter Setdateformat:kdefault_date_time_format];
NSDate *date = [Formatter datefromstring:string];
[Formatter release];
return date;
}
1970 to present the number of seconds converted into time display
-(NSString *) Bysecondgetdate: (NSString *) second{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[Formatter setdateformat:@ "yyyy mm month DD day HH:MM:SS"];
NSString *dateloca = [NSString stringwithformat:@ "%@", second];
Nstimeinterval Time=[dateloca Doublevalue];
NSDate *detaildate=[nsdate Datewithtimeintervalsince1970:time];
NSString *timestr = [Formatter stringfromdate:detaildate];
return timestr;
}
OBJECTC Time Usage