Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create event with NSDate #29

Closed
SwiftySrl opened this issue Dec 9, 2014 · 8 comments
Closed

How to create event with NSDate #29

SwiftySrl opened this issue Dec 9, 2014 · 8 comments

Comments

@SwiftySrl
Copy link

Hi developer;
I'm a developer as well, and I'm trying to implement inside JTCalendar some event taken from NsDate*dateSelected

In it I've some date in "dd/MM/yyyy HH:mm":

NSLog(@"%@",dateselected)
"2014-12-09 00:05:41.908 Database Demo[1348:31927] 2014-12-09 09:39:37 +0000
2014-12-09 00:05:41.911 Database Demo[1348:31927] 2014-12-14 09:32:26 +0000
2014-12-09 00:05:41.913 Database Demo[1348:31927] 2014-12-20 08:08:09 +0000"

When I try to implement the date of "dateSelected" in the eventsByDate[key], it makes just one event (the last one that I've insert in the dateSelected).

Here the code:

  • (void)createEvents
    {
    eventsByDate = [NSMutableDictionary new];

    NSDate *myDate = dateSelected;
    
    NSString *key = [[self dateFormatter] stringFromDate:myDate];
    
    if(!eventsByDate[key]){
        eventsByDate[key] = [NSMutableArray new];
    }
    
    [eventsByDate[key] addObject:myDate];
    }
    

    }

Can you help me ???

Have a nice day......;)

@jonathantribouharet
Copy link
Owner

If your key for eventsByDate used a date format like "dd/MM/yyyy HH:mm" it's normal because this format contains the hours so you don't have the events sort by date but also by hours. It works in the Example, you don't need to change the format of the date, it's only used for get back your events and nothing else.

@SwiftySrl
Copy link
Author

Ok i have change code in this:

  • (void)createEvents
    {
    eventsByDate = [NSMutableDictionary new];

    NSDate * myDate= dateSelected;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSString *convertedString = [dateFormatter stringFromDate:myDate];
    NSLog(@"Converted String : %@",convertedString);
    NSDate * outDate = [dateFormatter dateFromString: convertedString];

    NSString *key = [[self dateFormatter] stringFromDate:outDate];

    if(!eventsByDate[key]){
        eventsByDate[key] = [NSMutableArray new];
    } 
    [eventsByDate[key] addObject:outDate];   
    

    }


NSLog(@"Converted String : %@",convertedString);

2014-12-09 14:31:21.104 Database Demo[3017:106606] Converted String : 09-12-2014
2014-12-09 14:31:21.106 Database Demo[3017:106606] Converted String : 11-12-2014
2014-12-09 14:31:21.108 Database Demo[3017:106606] Converted String : 17-12-2014

But the result is not changed,it creates only the last one that I've insert in the dateSelected.

@jonathantribouharet
Copy link
Owner

It's because you don't understand what you're doing. Each time you call createEvents you do eventsByDate = [NSMutableDictionary new]; so each time you reset eventsByDate. You have to loop on the array of events that you have in createEvents, juste replace my loop but keep the structure.

@SwiftySrl
Copy link
Author

Ok i have insert a loop.This is a code:

  • (void)createEvents
    {
    eventsByDate = [NSMutableDictionary new];
    prova=[[NSMutableArray alloc]init];
    [prova addObject:dateSelected]; //Add date into array
    for(int i = 0; i < [prova count]; ++i)
    {
    NSDate * str = [prova objectAtIndex: i];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSString *convertedString = [dateFormatter stringFromDate:str];
    NSDate * outDate = [dateFormatter dateFromString: convertedString];
    NSString *key = [[self dateFormatter] stringFromDate:outDate];

    if(!eventsByDate[key]){
    eventsByDate[key] = [NSMutableArray new];
    }

    [eventsByDate[key] addObject:key];  
    

    }
    }

What am I doing wrong?

@jonathantribouharet
Copy link
Owner

Like i said, you don't understand what you do, with this code you will only have dateSelected in eventsByDate. You have to have an array, call it prova if you want, which have to contains all your events BEFORE you call createEvents.
I recommend you to ask your question on stackoverflow, this doesn't concern my library.

@SwiftySrl
Copy link
Author

Sorry but on stackOverflow they don't know your calendar.
To create events you use this code:

  • (void)createRandomEvents
    {
    eventsByDate = [NSMutableDictionary new];

    for(int i = 0; i < 30; ++i){
    NSDate *randomDate = [NSDate dateWithTimeInterval:(rand() % (3600 * 24 * 60)) sinceDate:[NSDate date]];
    NSString *key = [[self dateFormatter] stringFromDate:randomDate];
    if(!eventsByDate[key]){
    eventsByDate[key] = [NSMutableArray new];
    }
    [eventsByDate[key] addObject:randomDate];
    }

}

I need to replace your RandomDate with my dates that are contained in NSDate.
How can I do this?

@jonathantribouharet
Copy link
Owner

Sorry but like i said it doesn't depend of my calendar, it's not related.

@artemkalinovsky
Copy link

I've checked out how to do this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants