My date and time is 20-Nov-2019 21:09 Which is in UTC 24 hours format. now I want to convert it into local time in 12 hours formate. 30-Nov-2019 08:00 AM like this.
My code is :
// create dateFormatter with UTC time format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
// change to a readable time format and change to local time zone
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:date];
My code when i send my local time 12 formate into 24 hours UTC
-(NSString *)getUTCFormateDate:(NSDate *)localDate
{
// NSLog(#"%#", localDate);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm"];
NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
dateFormatter.locale = twelveHourLocale;
NSTimeInterval timeZoneoffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
NSTimeInterval utcTimeInterval = [localDate timeIntervalSinceReferenceDate] - timeZoneoffset;
NSDate *utcCurrentDate = [NSDate dateWithTimeIntervalSinceReferenceDate:utcTimeInterval];
NSString *dateString = [dateFormatter stringFromDate:utcCurrentDate];
// NSLog(#"dateString %#", dateString);
return dateString;
}
-(NSDate *)getUTCDate:(NSString *)currentDate{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MMM-yyyy HH:mm"];
NSDate *date1 = [dateFormat dateFromString:currentDate];
if (date1 == nil){
[dateFormat setDateFormat:#"dd-MMM-yyyy hh:mm a"];
date1 = [dateFormat dateFromString:currentDate];
}
return date1;
}
I think you are doing too much. The format "hh" is the hour in 12-hour format, HH is 24-hour format. You should not have to set the locale for that (though setting to en_US_POSIX does avoid the user's 24-hour preference in the [NSLocale currentLocale] instance which can override that on iOS).
NSDate is an absolute instance in time. You need to apply a calendar and time zone with an NSDateFormatter to get numeric year/month/day etc. values out of it, but you don't need to adjust the offset to the reference date (that is changing the actual date, not just reformatting it in a different time zone).
NSString *utcString = #"20-Nov-2019 21:09";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
formatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
formatter.dateFormat = #"dd-MMM-yyyy HH:mm";
NSDate *date = [formatter dateFromString:utcString];
formatter.timeZone = [NSTimeZone localTimeZone]; // GMT-5 for me
formatter.dateFormat = #"dd-MMM-yyyy hh:mm a";
NSLog(#"date: %#", [formatter stringFromDate:date]);
// date: 20-Nov-2019 04:09 PM
Related
How to get the current time based on TimeZone in objective c?
example-> Current time for Asia/Kolkata.
Try this..
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm"];
//Create the date assuming the given string is in GMT
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
//Create a date string in the local timezone
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone localTimeZone].secondsFromGMT];
NSString *localDateString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"date = %#", localDateString);
and if you want specific timezone date then see below code..
NSDate *myDate = [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateStyle:NSDateFormatterLongStyle];
[dateFormatter1 setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter1 setDateFormat:#"dd/MM/yyy hh:mm a"];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName: #"Asia/Calcutta"];
[dateFormatter1 setTimeZone:timeZone];
NSString *dateString = [dateFormatter1 stringFromDate: myDate];
NSLog(#"%#", dateString);
I currently get time as UTC and I want it to be convert into local time zone
NSDate * startDate;
Example strDate - 14/9/2017 7.28 Am
Required format - 14/9/2017 1.52 pm
I need help with objective c.
NSDate * startDate ; //contain utc date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, MMM d, yyyy - h:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:startDate];
but this is not correct time but date is correct
First convert NSString to NSDate.
NSDateFormatter *DateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZoneEDT =[NSTimeZone timeZoneWithName:#"GMT"];
[DateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[DateFormatter setTimeZone:timeZoneEDT];
[DateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS"];
NSDate *CurrentDate = [DateFormatter dateFromString:DateStr];
Then convert GMT time to local time.
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:#"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
NSString *dateRepresentation = [dateFormatter stringFromDate:dateFromString];
#athira try this. it works for me.
NSDate *startDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/M/yyyy h.mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:startDate];
NSLog(#"date = %#",timestamp); //date = 14/9/2017 4.34 PM
it will print current time with proper GMT+5:30 and i have used localTimeZone.
I want to log the server time in my iOS app , I am using NHNetworkTime library to get the network time, when I try to print the following code its showing GMT, through I have chosen UTC, how to print the date time in UTC?
NSDate* datetime = [NSDate networkDate];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]]; // Prevent adjustment to user's local time zone.
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS Z z "];
NSString* dateTimeInIsoFormatForZuluTimeZone = [dateFormatter stringFromDate:datetime];
NSLog(#"%#",dateTimeInIsoFormatForZuluTimeZone);
this is printing 2016-03-09 06:51:23.406 +0000 GMT
There is no time difference between Coordinated Universal Time (UTC) and Greenwich Mean Time(GMT). you can get more info from here More info
Refer :-
-(NSString *)getUTCFormateDate:(NSDate *)localDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:localDate];
[dateFormatter release];
return dateString;
}
Moreover to get time
-(NSString *)getTimeFromDate:(NSDate *)localDate
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
dateFormatter.dateFormat = #"MM/dd/yy";
NSString *dateString = [dateFormatter stringFromDate: localDate];
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc]init]autorelease];
timeFormatter.dateFormat = #"HH:mm:ss";
return [timeFormatter stringFromDate: localDate];
}
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"
I have locale date and time I am using the following code
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"d MMM yyyy hh:mm:ss a"];
NSString *currentTime = [dateFormatter stringFromDate:today];
NSLog(#"current time:%#",currentTime);
Here i got localTime
Now i am converting local time to GMT Time
I am using the following code
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"hh:mm:ss MM dd yyyy "];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter1 setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:currentTime];
NSDate *dateFromString = [dateFormatter dateFromString:currentTime];
Now my problem is i want to convert this GMT date into 10digit format like 1362468453
For that purpose i am using
[dateFormatter setDateFormat:#"d MMM yyyy hh:mm:ss a"];
How to get GMT Time in tendigits format
Use timeIntervalSince1970 function for your date to get epoch time.
NSLog(#"Epoch Time : %f", [dateFromString timeIntervalSince1970]);
Check this NSDate Class Reference