Currently I'm using the following code to acquire a date from a date picker. Right now, when I NSLog the NSDateComponents var, I get the numberical representation for a month (i.e. 7 for July). How can I print out the Month name, as well as the day of the week, like Saturday?
NSDate *selectedDate = _datePicker.date;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM:DD:HH:mm"];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:_datePicker.date];
NSLog(#"components: %#",components);
Use the format strings EEEE for the day of the week, and MMMM for the month of the year in your NSDateFormatter.
You might use monthSymbols from NSDateFormatter:
NSDate *selectedDate = _datePicker.date;
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:selectedDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *month = [[dateFormatter monthSymbols] objectAtIndex:components.month - 1];
Related
Im trying to figure out this timestamp format, but it looks weird to me.
There is a timestamp in that format: 184930.60
Explanation is: "This is UTC time since midnight in the form HH:MM:SS.SS."
It looks like number of seconds (based on change in file, for 20Hz GPS module), but it is 51h when you convert these seconds, and this can't be 51h after midnight.
Any idea how to convert that into nsdate?
Details about file format: https://racelogic.support/01VBOX_Automotive/01General_Information/Knowledge_Base/VBO_file_format
This is working example:
-(NSDate *)getDateFromDecimalTime:(double)decimalTime{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HHmmss.SS"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *date = [formatter dateFromString:[NSString stringWithFormat:#"%f", decimalTime]];
//gather current calendar
NSCalendar *calendar = [NSCalendar currentCalendar];
//gather date components from date
NSDateComponents *dateComponents = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
// Get current year/day/month
NSDate *now = [NSDate date];
// Specify which units we would like to use
unsigned units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSCalendar *calendar2 = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar2 components:units fromDate:now];
//set date components
[dateComponents setDay:[components day]];
[dateComponents setMonth:[components month]];
[dateComponents setYear:[components year]];
//save date relative from date
NSDate *completeDate = [calendar dateFromComponents:dateComponents];
return completeDate;
}
Is it possible to parse out date components from an NSString-based date? For instance, if I have an NSDateFormatter with yyyy-MM-dd HH:mm:ss.SSS ZZZ, how can I get an NSDateComponents object from this string directly?
Specifically, I'd like to preserve timezones, and differentiate this NSDateComponents object with one created from the yyyy-MM-dd format.
Not so directly, but with a few steps:
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS ZZZ"];
NSString *dateString = #"2016-03-15 12:00:00.000 +0800";
NSDate *date = [formatter dateFromString:dateString];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitTimeZone fromDate:date];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
NSTimeZone *timeZone = [components timeZone];
I would like to return the weekday name of tomorrow (i.e. if today is Sunday, I want Monday). Here's my code that yields today's weekday name.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEEE"];
weekDay = [formatter stringFromDate:[NSDate date]];
Rather than messing with NSCalendar, what would be a concise way of doing this (following the code I have here)?
Thank you.
user's language aware:
NSDateFormatter * df = [[NSDateFormatter alloc] init];
NSArray *weekdays = [df weekdaySymbols];
NSDateComponents *c = [[NSDateComponents alloc] init];
c.day = 1;
NSDate *tomorrow = [[NSCalendar currentCalendar] dateByAddingComponents:c
toDate:[NSDate date]
options:0];
c = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit
fromDate:tomorrow];
NSString *tomorrowname = weekdays[c.weekday-1];// the value of c.weekday may
// range from 1 (sunday) to 7 (saturday)
NSLog(#"%#", tomorrowname);
if you need to have the name in a certain language, add
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
after the creation of the date formatter.
Why are you afraid of "messing" with NSCalendar? For using the NSDateComponents methods, you will actually have to make use of NSCalendar. This is my solution for your problem.
// Get the current weekday
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
// !! Sunday = 1
NSInteger weekday = [weekdayComponents weekday];
Now, you can use the NSDateFormatter method -(NSArray *)weekdaySymbols which contains weekdays starting with Sunday at index 0. As the integer returned by [weekdayComponents weekday] starts with 0 for Saturday, we do not have to increase the value stored in weekday:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// !! Sunday = 0
NSUInteger symbolIndex = (weekday < 7) ? weekday : 0;
NSString *weekdaySymbol = [[formatter weekdaySymbols] objectAtIndex:(NSUInteger)symbolIndex];
I hope this was helpful although using NSCalendar to some extent.
EDIT: glektrik's solution is pretty straight ahead. But mind the following statement in the NSDate class reference of - dateByAddingTimeInterval:.
Return Value:
A new NSDate object that is set to seconds seconds relative to the receiver. The date returned might have a representation different from the receiver’s.
Thank you for your comment, Abizern. Here's what I did:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEEE"];
weekDay = [formatter stringFromDate:[NSDate date]];
NSDateComponents *tomorrowComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSDate *compDate = [[NSCalendar currentCalendar] dateFromComponents:tomorrowComponents];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
offsetComponents.day = 1;
NSDate *tomorrow = [[NSCalendar currentCalendar] dateByAddingComponents:offsetComponents toDate:compDate options:0];
nextWeekDay = [formatter stringFromDate:tomorrow];
Two NSString objects (weekDay and nextWeekDay) now store the days of the week names for today and tomorrow (currently Sunday and Monday).
This works well, but I wonder if there's an easier way. Objective-C dates are quite cumbersome :(
Thanks again.
I have an NSDate object *myDate.I can get the week day from this date and I want to set the weekday to an integer c and get the new date object in that week only.
I searched for it and I found the answer,here is the code..
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:1];
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:[NSDate date] options:0];
NSLog(#"new date==%#",newDate);
This will return date of the next day.
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"MMM dd, yyy"];
date = [formatter dateFromString:string];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:date];
NSInteger day = [components day];
NSInteger weekday = [components weekday]; // if necessary
I'm trying to figure out what day (i.e. Monday, Friday...) of any given date (i.e. Jun 27th, 2009)
Thank you.
I've been doing:
NSDateFormatter* theDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[theDateFormatter setDateFormat:#"EEEE"];
NSString *weekDay = [theDateFormatter stringFromDate:[NSDate date]];
This has the added bonus of letting you choose how you'd like the weekday to be returned (by changing the date format string in the setDateFormat: call
Lots more information at:
http://developer.apple.com/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369
You may have a look to the NSDate and NSCalendar classes.
For example, here and here
They provide the following code:
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
[gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [weekdayComponents day];
NSInteger weekday = [weekdayComponents weekday];
Use NSCalendar and NSDateComponents. As shown in the NSDateComponents documentation:
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date];
int weekday = [weekdayComponents weekday];
Here's what I wound up with, which works exactly as intended. It takes a date, alters the date to be the first of the month, and gets the day of that date:
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
NSDateComponents *monthDateComponents = [[NSCalendar currentCalendar] components: NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear |
NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate: date];
// build date as start of month
monthDateComponents.year = components.year;
monthDateComponents.month = components.month;
monthDateComponents.day = 1;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
// [gregorian setFirstWeekday:1];
NSDate *builtDate =[gregorian dateFromComponents: monthDateComponents];
// NSDateComponents *weekdayComponents =[gregorian components: NSCalendarUnitWeekday fromDate: builtDate];
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:#"E"];
NSString *firstDay = [df stringFromDate:builtDate];
if([firstDay isEqual:#"Sun"]) // do for the other 6 days as appropriate
return 7;