24 hour time from date string - ios

I am trying to get just the time and that too in 24 hour format from following string :
7/2/2015 2:30:00 PM
This is what I have tried :
-(NSString*)returnDate:(NSString*)dateString
{
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"MM/dd/yyyy hh:mm:ss aaa"];
NSDate *date = [dateFormatter1 dateFromString:dateString];
dateString = [date descriptionWithLocale:[NSLocale systemLocale]];
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"UTC"];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:date];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;
NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date];
NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
[dateFormatters setDateFormat:#"HH:mm a"];
[dateFormatters setDateStyle:NSDateFormatterShortStyle];
[dateFormatters setTimeStyle:NSDateFormatterShortStyle];
[dateFormatters setDoesRelativeDateFormatting:YES];
[dateFormatters setTimeZone:[NSTimeZone systemTimeZone]];
dateString = [dateFormatters stringFromDate: destinationDate];
return dateString;
}
Output : 4:30
This is returning correct time but in 12 hour format. I want the time in 24 hour format.
Desired output : 16:30

There is way to much code in the question, try this:
NSString *dateString = #"7/2/2015 4:30:00 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss aaa"];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"HH:mm"];
dateString = [dateFormatter stringFromDate: date];
NSLog(#"dateString: %#", dateString);
Output:
dateString: 16:30

Related

How to convert my server time to other country time?

I am getting time from server is 2016/07/19--12:20:00
Now I have to change this time according to country.
For example If I am in Auckland then time has to like 2016/07/19--18:50:00
I found so many solutions in stack overflow but I can't solve this.
Please help me
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY/MM/dd--HH:mm:ss"];
NSDate* sourceDate = [dateFormatter dateFromString:model.tripEndTime];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setDateFormat:#"YYYY/MM/dd--HH:mm:ss"];
NSString* localTime = [dateFormat stringFromDate:destinationDate];
NSLog(#"localTime:%#", localTime);
I tried with this code
Just convert the server time to UTC time, then convert to local country time. Just write a separate method for this
+(NSString*)convertLocaleTimeToChatTimeStampWithDateStr:(NSString *)strDate
{
NSDateFormatter *df = [DateHelper getDefaultTimeFormatter];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
[df setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date = [df dateFromString:strDate];
[df setDateFormat:#"dd MMM, hh:mm a"];
[df setTimeZone:[NSTimeZone localTimeZone]];
return [df stringFromDate:date];
}
+(NSDateFormatter*)getDefaultTimeFormatter
{
NSDateFormatter *df = [NSDateFormatter new];
[df setDateStyle:NSDateFormatterNoStyle];
[df setTimeStyle:NSDateFormatterShortStyle];
return df;
}
hope this will solve your problem. Thanks
It is better to use server timestamp. After getting timestamp convert it according to timezone

what is the reason for one day delay in date value?

below is my sample code.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate * dateFormatted = [dateFormatter dateFromString:#"2016-01-17"];
NSLog(#"Date : %d",dateFormatted);
The output is 2016-01-16 18:30:00 +0000. How to get date Formatt 2016-01-17 . what is the reason for one day delay. please help me.
You shouldn't create the NSDate from the NSString without timezone mark.
Add timezone to the NSString like "-0800":
e.g. "2016-01-17 -0800" --> "yyyy-MM-dd Z"
Add code:
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];// change timezone u want
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate * dateFormatted = [dateFormatter dateFromString:#"2016-01-17"];
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:dateFormatted];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"UTC"];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:dateFormatted];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;
NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:dateFormatted] ;
NSLog(#"Date : %#",destinationDate);
You can use below code...!
I am forcing the Date to be 2016-01-17 in first 2 scenarios. U can check and use if it is useful to you..!
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate * sourceDate = [dateFormatter dateFromString:#"2016-01-17"];
int daysToAdd = 1;
NSDate *presentDate = [sourceDate dateByAddingTimeInterval:60*60*24*daysToAdd]; //Output 2016-01-17 18:30:00 +0000
NSDate * dateFormatted = [dateFormatter dateFromString:#"2016-01-17"];
NSCalendar *calendar1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components1 = [calendar1 components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:dateFormatted];
[components1 setHour:24];//24
[components1 setMinute:00];//00
NSDate *presentDate1 = [calendar1 dateFromComponents:components1]; //Output 2016-01-17 18:30:00 +0000
//The input
NSString *myDateAsAStringValue = #"2016-01-17";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[df setDateFormat:#"yyyy-MM-dd"];
//Getting Date
NSDate *myDate = [df dateFromString: myDateAsAStringValue]; // Output 2016-01-17 00:00:00 +0000
//create the formatter for the output
NSDateFormatter *out_df = [[NSDateFormatter alloc] init];
[out_df setDateFormat:#"yyyy-MM-dd"];
NSString *dateStr=[out_df stringFromDate:myDate];//2016-01-17
Hope it helps you...!

how to convert a date String like "05/20/2015 05:27 pm" into NSDate then to timestamp

I am converting this date string "05/20/2015 05:27 pm" into NSDate with the help of formatter but it returns nil.
-(NSDate *)dateFromString:(NSString *)strDate withFormat:(NSString *)strDateFormat
{
NSDate *date = nil;
[self.dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
if (strDate) {
if (strDateFormat) {
[self.dateFormatter setDateFormat:strDateFormat];
}
date = [self.dateFormatter dateFromString:strDate];
}
return date;
}
I am using the above function where I am passing the string as "05/20/2015 05:27 pm" with format #"dd/MM/yyyy HH:mm:ss".
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"mm/dd/yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *stringDate = [dateFormatter dateFromString:#"05/20/2015 05:27 pm"];
NSLog(#"%#", stringDate);
-(NSDate *)dateFromString:(NSString *)strDate withFormat:(NSString *)strDateFormat
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:strDateFormat];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date = [dateFormatter dateFromString:strDate];
return date;
}
NSString *dateStr = #"05/20/2015 05:27 pm";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString:dateStr];
Nslog(#"%#",date);
To transform from NSDate to timestamp you can use:
NSDate *date = [NSDate date];
NSTimeInterval timestampSeconds = [date timeIntervalSince1970];
Result will be in seconds. To transform timestamp to standard milliseconds just multiply by 1000.
timeIntervalSince1970
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"mm/dd/yyyy hh:mm a"];
NSDate *strDate = [formatter dateFromString:#"05/20/2015 05:27 pm"];
NSLog(#"%#", strDate);//NSDate
time_t timeStamp = (time_t) [strDate timeIntervalSince1970];
NSLog(#"%ld", timeStamp);//timestamp
How to convert NSDate into unix timestamp iphone sdk?
This might helps you :)

How to Fetch date and time from a formatted string in iOS

I have a NSString "dateString" which contains date and time in given format.
"MM/dd/yyyy HH:mm:ss a".
I want to fetch the data from the dateString using NSDateFormatter and then I'm trying to show in a String "strDate". I.m able to fetch the data but the only issue is that i'm not getting the correct value for "hour" place.
For Ex:
dateString = 1/23/2015 7:06:37 AM
strDate = 01/23/2015 00:06:37 am
How to resolve this issue. Thanks in advance. This is my code:
-(NSString *)getDateQuestion:(NSString *)dateString
{
NSLog(#"dateString = %#",dateString);
//NSString to NSDate
NSDate *datefromString = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT+5:30"]];
[dateFormatter setTimeZone: [NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
datefromString = [dateFormatter dateFromString:dateString];
NSLog(#"dateString1 = %#",datefromString);
//NSDate convert to NSString
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT+5:30"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *strDate = [dateFormatter stringFromDate:datefromString];
NSLog(#"strDate = %#",strDate);
return strDate;
}
Just Pass Correct date Time string to the function and don't set time zone Unnecessary again and again... i hope it helps you...
NSDate *dat = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC+05:30"]];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
NSString * dateString =[dateFormatter stringFromDate:dat];
NSLog(#"dateString = %#",dateString);
NSLog(#"dateString1 = %#",dateString);
//NSDate convert to NSString
[dateFormatter setDateFormat:#"HH:mm:ss a"];
NSString *strDate = [dateFormatter stringFromDate:dat];
NSLog(#"strDate = %#",strDate);
HH stands for 24hr.
hh satnds for 12 hr.
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
Replace with "MM/dd/yyyy hh:mm:ss a"

date format is not working properly

I am converting date as per timezone and i have one more requirement is date format. Now i am getting date in 12/15/14, 5:00 AM format but i want it in “18th Nov. 2014 at 5:15 PM” format.Event I am not getting proper output as per my date format.
Is this possible in "Objective C".My code is :
-(NSString *)dateFormater : (NSString *)dateStr
{
//NSString *dateStr = #"2014-11-30T21:25:00+00:00";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssz"];
// [dateFormatter1 setDateFormat:#"dd MMM,yyyy'T'HH:mm:ssz"];
NSDate *date = [dateFormatter1 dateFromString:dateStr];
NSLog(#"date : %#",date);
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"UTC"];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:date];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;
NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date];
NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
// [dateFormatters setDateFormat:#"dd-MMM-yyyy hh:mm"];
[dateFormatters setDateFormat:#"dd MMM, yyyy hh:mm a"];
[dateFormatters setDateStyle:NSDateFormatterShortStyle];
[dateFormatters setTimeStyle:NSDateFormatterShortStyle];
[dateFormatters setDoesRelativeDateFormatting:YES];
[dateFormatters setTimeZone:[NSTimeZone systemTimeZone]];
dateStr = [dateFormatters stringFromDate: destinationDate];
NSLog(#"DateString : %#", dateStr);
return dateStr;
}
You format string dd MMM, yyyy hh:mm a is correct but after setting the format string you are changing it again.
Remove these lines:
[dateFormatters setDateStyle:NSDateFormatterShortStyle];
[dateFormatters setTimeStyle:NSDateFormatterShortStyle];
But in general I would not use a static format string, at least not if you are releasing the app world wide. Since most countries have different localization for presentation purposes you are better of using :
dateStr = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle];

Resources