Showing posts with label Date related.... Show all posts
Showing posts with label Date related.... Show all posts

1 Jul 2017

Convert milliseconds to NSDate...



Convert milliseconds to NSDate...



 NSDate *date = [NSDate dateWithTimeIntervalSince1970:(milliSeconds / 1000.0)];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"dd-MM-yyyy HH:mm"];

    NSString *newdate = [dateFormat stringFromDate:date];

31 Mar 2017

To Know Past & Future Like(Today,Yesterday, Tomorrow, one month ago, One month later etc)...



To Know Past & Future Like(Today,Yesterday, Tomorrow, one month ago, One month later etc)...



#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

- (NSString *)relativeDateStringForDate:(NSDate *)date;

@end

#import "ViewController.h"

@interface ViewController ()
{
    
    __weak IBOutlet UILabel *lblValue;
    __weak IBOutlet UIDatePicker *datePicker;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
//    NSString *dateString = @"2016-04-27T18:30:00Z";
//    
//    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//    [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
//    NSDate *theDate = [formatter dateFromString:dateString];
//    NSLog(@"The Date: %@", theDate);
    
    
}

- (IBAction)btnClicked:(id)sender
{
  lblValue.text = [self relativeDateStringForDate:datePicker.date];
}

- (NSString *)relativeDateStringForDate:(NSDate *)date
{
    
    NSCalendarUnit units = NSCalendarUnitDay | NSCalendarUnitWeekOfYear |
    NSCalendarUnitMonth | NSCalendarUnitYear;
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components1 = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekOfYear) fromDate:[NSDate date]];
    
    NSDate *today = [cal dateFromComponents:components1];
    
    components1 = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:date];
    NSDate *thatdate = [cal dateFromComponents:components1];
    
    // if `date` is before "now" (i.e. in the past) then the components will be positive
    NSDateComponents *components = [[NSCalendar currentCalendar] components:units
                                                                   fromDate:thatdate
                                                                     toDate:today
                                                                    options:0];
  
    if ( labs(components.year) > 0) {
        // year is most significant component
        if (components.year > 0) {
            // in the past
            return [NSString stringWithFormat:@"%ld years ago", (long)components.year];
        } else {
            // in the future
            return [NSString stringWithFormat:@"In %ld years", labs(components.year)];
        }
    }
    else if (labs(components.month) > 0)
    {
        if (components.month > 0) {
            // in the past
            return [NSString stringWithFormat:@"%ld months ago", (long)components.month];
        } else {
            // in the future
            return [NSString stringWithFormat:@"In %ld months", labs(components.month)];
        }
    }
    else if (labs(components.weekOfYear) > 0)
    {
        if (components.weekOfYear > 0) {
            // in the past
            return [NSString stringWithFormat:@"%ld weeks ago", (long)components.weekOfYear];
        } else {
            // in the future
            return [NSString stringWithFormat:@"In %ld weeks", labs(components.weekOfYear)];
        }
    }
    else if (labs(components.day) > 0)
    {
    if (components.day > 0) {
        if (components.day > 1)
        {
            return [NSString stringWithFormat:@"%ld days ago", (long)components.day];
        }
        else
        {
            return @"Yesterday";
        }
    } else {
        if (components.day < -1)
        {
            return [NSString stringWithFormat:@"In %ld days ", labs(components.day)];
        }
        else
        {
            return @"Tomorrow";
        }
    }
 }
    else {
           return @"Today";
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end



Date Picker & To find Today, YesterDay, Days ago, weeks ago,months ago,years ago from given Date...




Date Picker & To find Today, YesterDay, Days ago, weeks ago,months ago,years ago from given Date...




#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

- (NSString *)relativeDateStringForDate:(NSDate *)date;


@end



#import "ViewController.h"

@interface ViewController ()
{
    
    __weak IBOutlet UILabel *lblValue;
    __weak IBOutlet UIDatePicker *datePicker;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
//    NSString *dateString = @"2016-04-27T18:30:00Z";
//    
//    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//    [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
//    NSDate *theDate = [formatter dateFromString:dateString];
//    NSLog(@"The Date: %@", theDate);
    
}

- (IBAction)btnClicked:(id)sender
{
  lblValue.text = [self relativeDateStringForDate:datePicker.date];
}


- (NSString *)relativeDateStringForDate:(NSDate *)date
{
    
    NSCalendarUnit units = NSCalendarUnitDay | NSCalendarUnitWeekOfYear |
    NSCalendarUnitMonth | NSCalendarUnitYear;
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components1 = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
    NSDate *today = [cal dateFromComponents:components1];
    
    components1 = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:date];
    NSDate *thatdate = [cal dateFromComponents:components1];
    
    // if `date` is before "now" (i.e. in the past) then the components will be positive
    NSDateComponents *components = [[NSCalendar currentCalendar] components:units
                                                                   fromDate:thatdate
                                                                     toDate:today
                                                                    options:0];
    
    if (components.year > 0)
    {
        return [NSString stringWithFormat:@"%ld years ago", (long)components.year];
    }
    else if (components.month > 0)
    {
        return [NSString stringWithFormat:@"%ld months ago", (long)components.month];
    }
    else if (components.weekOfYear > 0)
    {
        return [NSString stringWithFormat:@"%ld weeks ago", (long)components.weekOfYear];
    }
    else if (components.day > 0)
    {
        if (components.day > 1)
        {
            return  [NSString stringWithFormat:@"%ld days ago", (long)components.day];
        }
        else
        {
            return @"Yesterday";
        }
    }
    else
    {
        return @"Today";
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


@end

8 Mar 2017

Different types of Date format that you can set...


Different types of Date format that you can set...


FormatMeaning/DescriptionExample1Example2
yA year with at least 1 digit.175 AD → “175”2016 AD → “2016”
yyA year with exactly 2 digits.5 AD → “05”2016 AD → “16”
yyyA year with at least 3 digits.5 AD → “005”2016 AD → “2016”
yyyyA year with at least 4 digits.5 AD → “0005”2016 AD → “2016”
MA month with at least 1 digit.July → “7”"November" → "11"
MMA month with at least 2 digits.July → “07”"November" → "11"
MMMThree letter month abbreviation.July → “Jul”"November" → "Nov"
MMMMFull name of month.July → “July”"November" → "November"
MMMMMOne letter month abbreviation(Jan,June,July all will have 'J').July → “J”"November" → "N"
dDay with at least one digit.8 → “8”29 → “29”
ddDay with at least two digits.8 → “08”29 → “29”
“E”, “EE”, or”EEE”3 letter day abbreviation of day name.Monday → “Mon”Thursday → “Thu”
EEEEFull Day name.Monday → “Monday”Thursday → “Thursday”
EEEEE1 letter day abbreviation of day name.(Thu and Tue will be 'T')Monday → “M”Thursday → “T”
EEEEEE2 letter day abbreviation of day name.Monday → “Mo”Thursday → “Th”
aPeriod of day (AM/PM).10 PM → “PM”2 AM → “AM”
hA 1-12 based hour with at least 1 digit.10 PM → “10”2 AM → “2”
hhA 1-12 based hour with at least 2 digits.10 PM → “10”2 AM → “02”
HA 0-23 based hour with at least 1 digit.10 PM → “14”2 AM → “2”
HHA 0-23 based hour with at least 2 digits.10 PM → “14”2 AM → “02”
mA minute with at least 1 digit.7 → “7”29 → “29”
mmA minute with at least 2 digits.7 → “07”29 → “29”
sA second with at least 1 digit.7 → “7”29 → “29”
ssA second with at least 2 digits.7 → “07”29 → “29”
There are many more, for getting different time based on zone(z), for getting time with millisecond details(S), etc.

Recent Posts

Codable demo

Link: https://www.dropbox.com/s/kw7c1kgv1628bh7/codableDemo.zip?dl=0