Follow by Email

Search This Blog

Tuesday, 29 September 2015

iOS NSDate -- All type of conversions Class Methods

//Get the current Device Date
+(NSDate *)getcurrentDate
{
    NSDate *CurrentDate = [NSDate date];
    NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [Formatter setLocale:locale];
    [Formatter setDateFormat:@"yyyy-MM-dd h:mm a"];
    return CurrentDate;
}

//Get Month Year in String Format  From  Date
+(NSString *)getStringMonthYearFromDate :(NSDate *)givendate
{
    
    NSCalendar *dateCalender = [NSCalendar currentCalendar];
    NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [Formatter setLocale:locale];
    [Formatter setDateFormat:@"MMMM-y"];
    NSString *FormattedDate = [Formatter stringFromDate:givendate];
    
    return FormattedDate;
}

+(NSDate *)getcurrentMonthYear
{
    NSDate *CurrentDate = [NSDate date];
    NSCalendar *dateCalender = [NSCalendar currentCalendar];
    NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [Formatter setLocale:locale];
    [Formatter setDateFormat:@"MMMM y HH"];
    NSString *FormattedDate=[Formatter stringFromDate:CurrentDate];
    
    NSDate *actualDate =[Formatter dateFromString:FormattedDate];
    
    NSDateComponents *components = [dateCalender components: NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear |NSCalendarUnitHour fromDate:actualDate];

    [components setCalendar:dateCalender];
    
    [components setDay:1];
    [components setHour:4];
    
    actualDate = [dateCalender dateFromComponents:components];
    return actualDate;
}

+(NSDate *)getcurrentDateOnly
{
    NSDate *CurrentDate = [NSDate date];
    NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [Formatter setLocale:locale];
    [Formatter setDateFormat:@"dd-MMM-yyyy"];
    NSString *FormattedDate=[Formatter stringFromDate:CurrentDate];
    return [Formatter dateFromString:FormattedDate];
}

+(NSString *)getcurrentDateInString
{
    NSDate *CurrentDate = [NSDate date];
    NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [Formatter setLocale:locale];
    [Formatter setDateFormat:@"dd-MMM-yyyy HH:mm a"];
    NSString *FormattedDate=[Formatter stringFromDate:CurrentDate];
    return FormattedDate;
}

+(NSDate *)getcurrentTime
{
    NSDate *CurrentDate = [NSDate date];
    NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [Formatter setLocale:locale];
    [Formatter setDateFormat:@"h:mm a"];
    return CurrentDate;
}

+(NSDate *)convertStringDateToNSDate :(NSString *)dateString
{

    NSCalendar *currentCalender =[NSCalendar currentCalendar];

    NSDateFormatter *stringToDateFormater =[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [stringToDateFormater setLocale:locale];
    [stringToDateFormater setDateFormat:@"dd MMMM y"];
    
    NSDate *fulldate = [stringToDateFormater dateFromString:dateString];
    
    NSDateComponents *components = [currentCalender components: NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear |NSCalendarUnitHour fromDate:fulldate];

    NSCalendar *dateCalender = [NSCalendar currentCalendar];
    [components setCalendar:dateCalender];

    [components setDay:1];
    [components setHour:4];
    fulldate = [dateCalender dateFromComponents:components];

    return fulldate;
}

+(NSDate *)convertStringTimeToDate :(NSString *)time
{
    NSDate *CurrentDate = [NSDate date];
    NSCalendar *currentCalender =[NSCalendar currentCalendar];
    
    NSDateComponents *components = [currentCalender components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:CurrentDate];
    
    NSDateFormatter *stringToDateFormater =[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [stringToDateFormater setLocale:locale];
    [stringToDateFormater setDateFormat:@"h:mm a"];
    
    NSDate *date = [stringToDateFormater dateFromString:time];
    
    NSCalendar *dateCalender =[NSCalendar currentCalendar];
    
    NSDateComponents *datecomponents = [dateCalender components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:date];
    
    [datecomponents setDay:components.day];
    [datecomponents setMonth:components.month];
    [datecomponents setYear:components.year];
    date = [dateCalender dateFromComponents:datecomponents];
    return date;
}

+(NSString *)convertTimeToString :(NSDate *)time
{
    NSDate *CurrentDate = time;
    NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [Formatter setLocale:locale];
    [Formatter setDateFormat:@"h:mm a"];
    NSString *timeString=[Formatter stringFromDate:CurrentDate];
    return timeString;
}

+(NSString *)convertDateToString :(NSDate *)date
{
    NSDate *CurrentDate = date;
    NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [Formatter setLocale:locale];
    [Formatter setDateFormat:@"MMM"];
    NSString *timeString=[Formatter stringFromDate:CurrentDate];
    return timeString;
}

+(NSString *)getcurrentTimeInString
{
    NSDate *CurrentDate = [NSDate date];
    NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [Formatter setLocale:locale];
    [Formatter setDateFormat:@"h:mm a"];
    NSString *timeString=[Formatter stringFromDate:CurrentDate];
    
    
    return timeString;
}

+(NSString *)convertMinutesToTimeInterval :(NSUInteger)minutes
{
    NSTimeInterval timeInterval =minutes*60;
    NSDateComponentsFormatter *componentFormatter = [[NSDateComponentsFormatter alloc] init];
    
    componentFormatter.unitsStyle = NSDateComponentsFormatterUnitsStyleShort;
    componentFormatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorDropAll;
    
    NSString *formattedString = [componentFormatter stringFromTimeInterval:timeInterval];
    return formattedString;
}

+(NSString *)convertMinutesToTimeIntervalNumericWay :(NSUInteger)minutes
{
    NSTimeInterval timeInterval =minutes*60;
    NSDateComponentsFormatter *componentFormatter = [[NSDateComponentsFormatter alloc] init];
    
    componentFormatter.unitsStyle = NSDateComponentsFormatterUnitsStylePositional;
    componentFormatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorDropAll;
    
    NSString *formattedString = [componentFormatter stringFromTimeInterval:timeInterval];
    return formattedString;

}

1 comment:

  1. Thank you for sharing this help. It is really very helpful and very useful post. It worth reading.
    Web Design Fort Lauderdale

    ReplyDelete