OBJECTC Time Usage

Source: Internet
Author: User

#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

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.