Retrieve Year and Month from string of date - objective-c

I want to retrieve year and then month from this kind of date: 2011-12-23 10:45:01 with no luck.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormatter setDateFormat:#"yyyy"];
NSLog(#"Date = %#",[dateFormatter dateFromString:#"2011-12-23 10:45:01"]);
[dateFormatter release];
Date = (null), i can't understand why.

You have to do it in two steps, first match the whole date, then output the bits you want:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:#"2011-12-23 10:45:01"];
//now you have the date, you can output the bits you want
[dateFormatter setDateFormat:#"yyyy"];
NSString *year = [dateFormatter stringFromDate:date];
[dateFormatter setDateFormat:#"MM"];
NSString *month = [dateFormatter stringFromDate:date];
[dateFormatter release];

This was already answered in Formatting NSDate into particular styles for both year, month, day, and hour, minute, seconds.
Specifically:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH:mm:ss"];
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:now];
NSString *theTime = [timeFormat stringFromDate:now];
NSLog(#"\n"
"theDate: |%#| \n"
"theTime: |%#| \n"
, theDate, theTime);
[dateFormat release];
[timeFormat release];
[now release];

Related

Objective C - Convert DateTime to Local DateTime

I am pretty new to Objective C and it has been horrible experience to get the current device datetime in systemTimeZone. This is what I have:
NSDate *now = [NSDate date];
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[DateFormatter setDateFormat:#"MM-dd-yyyy HH:mm"];
NSString *currentDateTime = [DateFormatter stringFromDate:now];
NSDate *curDate = [DateFormatter dateFromString:currentDateTime];
Line 5 NSString has the correct local datetime in currentDateTime string variable. But Line 6 again switches back to UTC DateTime. I do not understand why it would switch back to UTC even though the DateFormatter has the systemTimezone set. Can you please help me find out what is that I am missing?
Just set the timeZone in the dateFormatter, This code is enough
NSString *dateString = #"24 08 2011 09:45PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MM yyyy hh:mma"];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"BST"];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
The dateFromString will now have the date 24 08 2011 08:45PM(GMT).. Then to convert this to string with local time just code the following,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
[formatter setLocale:[NSLocale systemLocale]];
[dateFormatter setDateFormat:#"dd MM yyyy hh:mma"];
NSString *stringFromDAte = [dateFormatter stringFromDate:dateString];
or You can also try this one:
NSTimeInterval seconds; // assume this exists
NSDate* ts_utc = [NSDate dateWithTimeIntervalSince1970:seconds];
NSDateFormatter* df_utc = [[[NSDateFormatter alloc] init] autorelease];
[df_utc setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df_utc setDateFormat:#"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease];
[df_local setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[df_local setDateFormat:#"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSString* ts_utc_string = [df_utc stringFromDate:ts_utc];
NSString* ts_local_string = [df_local stringFromDate:ts_utc];
// you can also use NSDateFormatter dateFromString to go the opposite way
Table of formatting string parameters:
https://waracle.com/iphone-nsdateformatter-date-formatting-table/
If performance is a priority, you may want to consider using strftime
https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/strftime.3.html

NsDateFormatter format the date

How can i this format this date ?
2/24/2017 11:13:43 AM
i tried this code but not worked
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
NSString *stringDate = [dateFormatter stringFromDate:createdDate];
You need to change the dateformate. You need to 'a' into formater.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss aa"];
NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]];
Output:
Printing description of stringDate:
02/25/2017 09:56:42 AM
You can see more about dateFormate.
Cheers

Time from a TextView into NSDate

I'm building an iPhone app to take times: a car has to get at a given time at a point, so if the car is sooner or later, gets points.
I need to obtain the most precise timing as I can, so I want to use milliseconds, but as far results are disappointing.
The time is typed on a TextField, let's say 12:17:22 and the code is the following:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:#"HH:mm:ss"];
timeLegStart = [NSDate new];
timeLegStart = [dateFormatter dateFromString:setStartTime.text];
NSLog(#"timeLegStart = %#",timeLegStart);
The output is 1970-01-01 11:17:22 +0000, and I'd like it to be 12:17:22:000
Set your NSDateFormatter's properties like this (replacing with your time zone info):
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"pt_BR"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"America/Sao_Paulo"]];
[dateFormatter setDateFormat:#"HH':'mm':'ss' 'z"];
[dateFormatter setLocale:locale];
The problem here is that NSDate comprises of date and time and they are not separately available. When you are giving the input date with time specs only it load the calendar start date values with the time you provided and creates a date. Hence here comes the date as output.
Actually there is no problem to work with that.
You can use the same formatter to get the time retrieved back:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSDate* timeLegStart = [NSDate new];
timeLegStart = [dateFormatter dateFromString:#"12:12:12"];
NSLog(#"timeLegStart = %#",[dateFormatter stringFromDate:timeLegStart]);
It will give you:
timeLegStart = 12:12:12
EDIT : when logging NSDate it will be in GMT format only.Since you set the timezone to local there date will be always displayed like what you got.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSDate* timeLegStart = [NSDate new];
timeLegStart = [dateFormatter dateFromString:#"12:12:12"];
NSLog(#"timeLegStart = %#",timeLegStart);
Will give you:
2000-01-01 12:12:12 +0000
Well, I've sorted it out this way:
- (void)fijaTiempoInicio
{
//getting current day
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:~ NSTimeZoneCalendarUnit fromDate:[NSDate date]];
[dateComponents setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Spain"]];
NSDate *currentDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
//extracting year, month and day from current date
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"dd"];
NSString *currentDayString = [NSString stringWithFormat:#"%#",[df stringFromDate:currentDate]];
[df setDateFormat:#"MM"];
NSString *currentMonthString = [NSString stringWithFormat:#"%#",[df stringFromDate:currentDate]];
[df setDateFormat:#"yyyy"];
NSString *currentYearString = [NSString stringWithFormat:#"%#", [df stringFromDate:currentDate]];
NSDateFormatter *ft = [[NSDateFormatter alloc]init];
[ft setTimeZone:[NSTimeZone localTimeZone]];
[ft setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *tempDate = [NSString stringWithFormat:#"%#-%#-%# %#",currentYearString,currentMonthString,currentDayString,txtTiempoInicioTramo.text];
NSLog(#"tempDate= %#",tempDate);
startTime = [ft dateFromString:tempDate];
NSLog(#"startTime= %#",startTime);
Then, anothe method calculate the difference between two NSDates, which is a figure in milliseconds -a float
- (IBAction)btnCapturarTiempo:(id)sender
{
NSDateFormatter *formatCarTime = [[NSDateFormatter alloc]init];
[formatCarTime setDateFormat:#"HH:mm:ss"];
[formatCarTime setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Madrid"]];
carTime = [NSDate date];
difference = [carTime timeIntervalSinceDate:startTime];
labelTiempoCoche.text = [NSString stringWithFormat:#"%f",difference];

NSDateFormatter returning nil

can anyone please tell me why this code snippet returns nil in my log file:
NSString *dateString = [[NSString alloc] initWithString: #"2011-08-23 15:00"];
NSLocale *englishLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY-mm-dd HH:MM"];
[dateFormatter setLocale:englishLocale];
NSLog(#"%#",[dateFormatter dateFromString:dateString]);
Thanks a lot.
Check my variant:
NSString *dateString = [[NSString alloc] initWithString: #"2011-08-23 15:00"];
NSLocale *englishLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//HH:MM -> HH:mm
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
[dateFormatter setLocale:englishLocale];
NSLog(#"%#",[dateFormatter dateFromString:dateString]);
As you see I've changed date format to : #"yyyy-MM-dd HH:mm"
All formats can be found here: UNICODE LOCALE DATA MARKUP LANGUAGE (LDML)
you should be correct 'HH:MM' to 'HH:mm'
NSString *dateString = [[NSString alloc] initWithString: #"2011-08-23 15:00"];
NSLocale *englishLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//HH:MM -> HH:mm
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
[dateFormatter setLocale:englishLocale];
NSLog(#"%#",[dateFormatter dateFromString:dateString]);
http://www.picksourcecode.com/ps/ct/161110.php
For more dateFormats

Date with Month name Objective-C

I have an NSDate object with value for example: "2011-08-26 14:14:51", I want to display "8 August 2011" how to implement it? Thanks.
NSDate *date = [NSDate date]; //I'm using this just to show the this is how you convert a date
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterLongStyle]; // day, Full month and year
[df setTimeStyle:NSDateFormatterNoStyle]; // nothing
NSString *dateString = [df stringFromDate:date]; [df release];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"dd LLLL yyyy"];
NSString *formattedDate = [df stringFromDate:[NSDate date]];
NSString *dateStr = #"2011-08-26 14:14:51";
//if you have date then,
NSString *dateStr = [NSString stringWithFormat:#"%#",yourDate];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat setDateFormat:#"MMMM dd, YYYY"];
NSString* temp = [dateFormat stringFromDate:date];
[dateFormat release];
NSLog(#"%#",temp);