1

I know this is a repetitive query, but I am stuck up with this issue. I need to know how can I get the day like Mon,Tues for a particular date eg: 18 July, 2010 etc.

appreciate your help!!

Manoj
  • 953
  • 2
  • 8
  • 24
  • Maybe this helps http://stackoverflow.com/questions/3694867/nsdate-get-year-month-day? – Mat Jul 18 '11 at 16:05
  • Are you trying to print out "Mon", if that is the case, you'll want to leverage the NSDateFormatter – Greg Martin Jul 18 '11 at 16:05
  • @Greg: I just wrote in abbreviation but it should be Monday, Tuesday etc. – Manoj Jul 18 '11 at 16:15
  • Right, I mean are you looking for the textual version rather than the numeric representation. Check out the documentation for NSDateFormatter. There are formatting strings that will give you pretty much any variation of what you are asking for. – Greg Martin Jul 18 '11 at 16:18
  • @Greg: It should be in numeric representation. eg: Sunday = 0, Monday =1, etc. – Manoj Jul 18 '11 at 16:20
  • In that case, this is a duplicate and the link @Mat posted should help you. – Greg Martin Jul 18 '11 at 16:24
  • That's not going to help me. Thanks anyways. – Manoj Jul 18 '11 at 16:29

3 Answers3

1

To print out the textual representation of a the day from and NSDate, try the following:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE"];
[dateFormatter release];

NSDate *myDate = [NSDate date];

NSString *day = [dateFormatter stringFromDate:myDate];

For more information on http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns

Greg Martin
  • 5,074
  • 3
  • 34
  • 35
0

try this

NSDateFormatter* theDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
    [theDateFormatter setDateFormat:@"EEEE"];
    NSString *weekDay =  [theDateFormatter stringFromDate:[NSDate date]];
iOSPawan
  • 2,884
  • 2
  • 25
  • 50
0
NSString *dateString = @"20 July, 2011"; // select this from your database

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd MMMM, yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSWeekdayCalendarUnit fromDate:date];
NSInteger weekday = [components weekday];
NSLog(@"%d", weekday);

Note that Sunday is 1.
2 would be Monday etc...

Edited my answer to show you an example of how to convert a string to an NSDate. ;)

Christian Schnorr
  • 10,768
  • 8
  • 48
  • 83
  • pawan.mangal's solution would return the weekday as a string, probably even localized. However, if you'd want to store it in a database or something similar, an integer might be more useful. – Christian Schnorr Jul 18 '11 at 16:13
  • You are checking every time the current date, lets say I select 11 December 2007 from my list, I wanted to what is the day number for that particular day. Hope I am clear to you. – Manoj Jul 18 '11 at 16:26
  • Well, you'd have to convert that string (?) to an NSDate and use it instead of [NSDate date]; ;) – Christian Schnorr Jul 18 '11 at 16:29
  • There are enough codes on SO to show you how to create a NSDate object for another date then the current one. – vikingosegundo Jul 18 '11 at 16:32
  • Added an example of how to get an NSDate from a string. – Christian Schnorr Jul 18 '11 at 16:42
  • Thanks Jenox.That was really helpful,though it was dumb to ask converting string to NSDate. – Manoj Jul 18 '11 at 17:32