I'm getting date as 2012-12-24 08:59:00 +0000 and i want it as 2012-12-20T16:00:00.000Z
i tried with this code
NSDateComponents *dayComponent = [[NSDateComponents alloc] init] ;
NSDate* now = [NSDate date];
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDateFormatter* formatter = [NSDateFormatter alloc];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
dayComponent.day = -2;
NSDate* fromDate = [theCalendar dateByAddingComponents:dayComponent toDate:now options:0];
fromDateStr = [formatter stringFromDate:fromDate];
my requirement is to get date of previous days like before 2 days, before 14 days etc in specified format.
but i'm getting fromDateStr as empty string.
Any suggestion would be appreciated.
The date format for 2012-12-20T16:00:00.000Z is
yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
Hope that helps.
Let me know if that does not give you the desired result.
Here is a sample code that worked for me:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSString *str = [formatter stringFromDate:[NSDate date]];
NSLog(#"Date = %#",str);
The output was:
Date = 2012-12-26T14:55:36.702Z
Related
i want to convert nsstring to nsdate
i have string have this value
08:00:00
i want to convert it so i wrote this code
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSDate *startDate = [formatter dateFromString:_startTime];
NSLog(#"%#",startDate);
_startTime have this value = 08:00:00
but startDate after i display it , it had this value
2000-01-01 06:00:00 +0000
Update I wrote this code
NSString *dateString = _startTime;
NSDateFormatter *formatter= [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[formatter setDateFormat:#"HH:mm:ss"];
NSDate *startDate = [[NSDate alloc] init];
// voila!
startDate = [formatter dateFromString:dateString];
NSLog(#"%#",startDate);
i want the output be 08:00:00
You can't have NSDate without a date !
if you want to access the time from an NSDate you'll need to use NSDateComponents (or NSTimeInterval).
To access the time from your NSDate using NSDateComponents :
NSString *startTime = #"08:00:00";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSDate *startDate = [formatter dateFromString:startTime];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:startDate];
NSLog(#"%d:%d:%d", [components hour], [components minute], [components second]);
I don't get what's the purpose of changing the string _startTime, do you want to compare dates ?
Edit :
NSString *startTime = #"08:00:00";
NSString *endTime = #"23:00:00";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSDate *startDate = [formatter dateFromString:startTime];
NSDate *endDate = [formatter dateFromString:endTime];
NSTimeInterval difference = [endDate timeIntervalSinceDate:startDate];
NSInteger temp = difference;
NSDate* newDate = [[NSDate alloc] init];
for (int i = 0; (i < difference && [newDate compare:startDate] == NSOrderedDescending); ++i)
{
newDate = [startDate dateByAddingTimeInterval:(temp - i*60*10)];
NSLog(#"%#",newDate);
}
run this and use NSDateComponents to log what you want !
Use this code i think this one help you..
NSString *dateStr = #"08:00:00";
NSDateFormatter *datFormatter = [[NSDateFormatter alloc] init];
[datFormatter setDateFormat:#"HH:mm:ss"];
NSDate* mydate = [datFormatter dateFromString:dateStr];
NSLog(#"date: %#", [datFormatter stringFromDate:mydate]);
i think this is what you really want...
I have NSString *localized_Date = minutesSinceMidnightToLocalizedTime (time units)
I get this string as 4:30 am, 10.00pm
how do I convert this string to NSDate?
PLease let me know.
I know it's a bit long code, but I wanted to make it as readable as possible.
This code uses minutesSinceMidnight instead of the localized string.
NSDate *currentDate = [NSDate date];
NSDateComponents *currentDateComponents = [calendar components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:currentDate];
NSDateComponents *currentDateToMidnight = [[NSDateComponents alloc] init];
[currentDateToMidnight setHour:-[currentDateComponents hour]];
[currentDateToMidnight setMinute:-[currentDateComponents minute]];
[currentDateToMidnight setSecond:-[currentDateComponents second]];
NSDate *midnight = [calendar dateByAddingComponents:currentDateToMidnight toDate:currentDate options:0];
NSDateComponents *midnightToDesiredDate = [[NSDateComponents alloc] init];
[midnightToDesiredDate setMinute:minutesSinceMidnight];
NSDate *desiredDate = [calendar dateByAddingComponents:midnightToDesiredDate toDate:midnight options:0];
NSDateFormatter is your friend, use the formats that suits your date/time
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:[NSString stringWithFormat:#"dd MMM yyyy HH:mm"]];
NSDate *_date = [df dateFromString:string];
Johan
Try this code:
NSString *dateString = #"4:30 am";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a"];
NSDate *date = [dateFormatter dateFromString:dateString];
Hope this will help you.
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];
What I'm looking for is the day of the month, example 2, 5, 30 or 31. In integer form, and not a string. I'm not looking for a programmed date, but today's date in whatever local they are in.
All the above answers are bad ways to accomplish this. This is the right way:
NSDate *date = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSDayCalendarUnit fromDate:date];
NSInteger day = components.day;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"d"];
NSInteger day = [[dateFormat stringFromDate:[NSDate date]] intValue];
Start with an NSDate then run it through an NSDateFormatter.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
Here is something that may work :
NSDate *theDate;
// Set theDate to the date you want
// For example, theDate = [NSDate date]; to get today's date
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"d";
NSString *theDayString = [formatter stringFromDate:theDate];
int theDay = theDayString.intValue;
NSLog (#"%d", theDay);
theDay contains the value you're looking for !
I have a date in string format just like 30-11-2012. I want the date next to it like 01-12-2012 again in string format.
Is there any way of getting it?
You should use NSCalendar for best compatibility
NSDate *today = [NSDate dateWithNaturalLanguageString:#"2011-11-30"];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dayComponent = [NSDateComponents new];
dayComponent.day = 1;
today = [calendar dateByAddingComponents:dayComponent toDate:today options:0];
//2011-12-01 12:00:00 +0000
I got the answer by someone I forget the name, I was about to accept his/her answer but when I clicked on accept answer it told me that the post bas been removed.
The answer given by that anonymous person was given below
NSString *dateString = #"22-11-2012";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSDateComponents *components= [[NSDateComponents alloc] init];
[components setDay:1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *dateIncremented= [calendar dateByAddingComponents:components toDate:dateFromString options:0];
NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *stringFromDate = [myDateFormatter stringFromDate:dateIncremented];
NSLog(#"%#", stringFromDate);
In the end I really like to thanks that anonymous person, I know this was not as much tricky but this helped me a lot. Thanks again you the Anonymous Helper.
swift 3.0
var components = DateComponents()
components.day = 1
let now = Date()
let futureDate = Calendar.current.date(byAdding: components, to: now, wrappingComponents: false)
//now = 2017-03-22 futureDate = 2017-03-23