I want to get timezone of user in String formatted like UTC+5:30
I have tried to get like this.
NSTimeZone *timeZone=[NSTimeZone localTimeZone];
NSLog(#"%# || %#",timeZone.abbreviation,timeZone.name);
This gives me this output.
(but not getting time as i want)
IST || Asia/Kolkata
Thanks
You can use NSDate Formatter to get the offset from UTC:
NSDateFormatter *dates = [[NSDateFormatter alloc]init];
[dates setDateFormat:#"Z"];
NSLog(#"%#", [dates stringFromDate:[NSDate date]]);
This will log: +0530 (+5:30)
If you want it in your specific format you can simply do:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"Z"];
NSString *offetString = [dateFormatter stringFromDate:[NSDate date]];
NSString *formattedString = [NSString stringWithFormat:#"UTC%#:%#",[offetString substringWithRange:NSMakeRange(0, 3)],[offetString substringWithRange:NSMakeRange([offetString length] - 2, 2)]];
NSDateFormatter *df = [[NSDateFormatter alloc]init];
df.timeZone = [NSTimeZone localTimeZone];
df.dateFormat = #"Z";
NSString *localTimeZoneOffset = [df stringFromDate:[NSDate date]];
NSLog(#"%#",localTimeZoneOffset);
You will get output like,
+0530 for UTC+5:30.
Update : (As asked in comment)
Try below code for your desired format,
NSDateFormatter *df = [[NSDateFormatter alloc]init];
df.timeZone = [NSTimeZone localTimeZone];
df.dateFormat = #"Z";
NSString *localTimeZoneOffset = [df stringFromDate:[NSDate date]];
NSLog(#"%#",localTimeZoneOffset);
NSString *one = [localTimeZoneOffset substringToIndex:1];
NSString *two = [localTimeZoneOffset substringWithRange:NSMakeRange(1, 2)];
NSString *three = [localTimeZoneOffset substringWithRange:NSMakeRange(3, 2)];
NSString *result = [NSString stringWithFormat:#"utc%#%#:%#",one,two,three];
NSLog(#"result : %#",result);
Related
Please how do I make my stringWithFormat/initWithFormat to display am/pm instead of 24hrs, even though users selects am/pm from the pickerviewer? When a user select time; eg 4:15 pm, instead of it to display 4:15pm as confirmation, it displays 16:15 instead. I want it as 4:15pm. PLEASE HELP!!
-(IBAction)datePickerValueChanged:(id)sender
{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"h:mm:ss a"];
NSString *strDate = [dateFormatter stringFromDate:self.pick.date];
dateArray = [strDate componentsSeparatedByString:#":"];
NSDate *current = [NSDate date];
NSString *currentTime = [dateFormatter stringFromDate:current];
NSArray *currArray = [currentTime componentsSeparatedByString:#":"];
curr = [currArray[0] intValue]*60 + [currArray[1] intValue];
min = [currArray[0] intValue]*60 + [currArray[1] intValue] + 30;
next = [[dateArray objectAtIndex:0] intValue]*60 +[[dateArray objectAtIndex:1] intValue];
....This is where values are displayed:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
time = [[NSString alloc]initWithFormat:#"%#T%#:%#:00",strDate, [dateArray objectAtIndex:0], [dateArray objectAtIndex:1]];
NSString *msg = [[NSString alloc] initWithFormat:#"%# %# %# %# ?", NSLocalizedString(#"do_ticket", nil), self.category.description,NSLocalizedString(#"at", nil),[[NSString alloc]initWithFormat:#"%#:%#", [dateArray objectAtIndex:0], [dateArray objectAtIndex:1]]];
alert_tick = [[UIAlertView alloc] initWithTitle:nil
message:msg
delegate:self
cancelButtonTitle:nil
otherButtonTitles:NSLocalizedString(#"Yes", nil), NSLocalizedString(#"No", nil), nil];
[alert_tick show];
}
Use NSDateFormatter. It doesn't matter that server values are given in 24h format, because your date formatting string can accommodate that (i.e. instead of using "h" for hours when calling setDateFormat:, which represents am/pm format, use "H" for 24h format).
Use the format #"hh:mm a" instead.
Example:
NSDate *today = [NSDate dateWithTimeIntervalSinceNow:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm a"];
NSDate *formattedDate = [formatter stringFromDate:today];
NSLog(#"date : %#", formattedDate);
Output:
date : 09:58 AM
I'm very new to this ios. I want to convert 2014-05-28T10:51:40.3056+01:00 to 28-may-2014.But I'm getting null. please help me .
//here dateString is 2014-05-28T10:51:40.3056+01:00
NSString * dateString= [dict objectForKey:#"created_date"];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
[df setDateFormat: #"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *inputDate = [df dateFromString: dateString];
[df setDateFormat:#"dd-MMM-yy"];
NSString * inputValue = [df stringFromDate:inputDate];
Try this :
NSString * dateString = #"2014-05-28T10:51:40.3056+01:00";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
[df setDateFormat: #"yyyy-MM-dd'T'hh:mm:ss.SSSZ"];
NSDate *inputDate = [df dateFromString: dateString];
[df setDateFormat:#"dd-MMM-yy"]; // [df setDateFormat:#"dd-MMM-yyyy"];
NSString *inputValue = [df stringFromDate:inputDate];
Output: 28-May-14
Well, I have been facing the same issue but then I split the Date string from . because it's not take the formate
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
if ([dateFormat dateFromString:[[value componentsSeparatedByString: #"-"] objectAtIndex:0]] == nil) {
value = [dateFormat dateFromString:[[value componentsSeparatedByString: #"-"] objectAtIndex:0]];
} else {
value = [dateFormat dateFromString:[[value componentsSeparatedByString: #"."] objectAtIndex:0]];
}
I have date in object "start_time"(2000-12-11T00:00:00+0000), but i need to print date and time separate to textfields. But when i tried with this code i got null value on console
__block NSDictionary *events = [[e.response objectForKey:#"events"] objectAtIndex:0];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// NSDate *dates = [events objectForKey:#"start_time"];
// NSDate *localDate = [NSDate date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init] init];
dateFormatter.dateFormat = #"MMMM dd yyyy";
NSString *dateString = [dateFormatter stringFromDate: [events objectForKey:#"start_time"]];
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] init];
timeFormatter.dateFormat = #"HH:mm:ss ZZZZ";
NSString *timeString = [timeFormatter stringFromDate: [events objectForKey:#"start_time"]];
NSLog(#"This is time------------------------------: %#",dateString); // null returns
//
This is how i store date into "start_time" by textfields
Blockquote
NSString *datestring = self.date.text;
NSString *timestring = self.time.text;
NSString *combined = [NSString stringWithFormat:#"%# %#", datestring, timestring];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:05.30]];
[dateFormat setDateFormat:#"MM dd yyyy HH:mm a"];
NSDate *date = [dateFormat dateFromString:combined];
NSDictionary *data = #{
#"start_time":[NSString stringWithFormat:#"%#",date]};
I think that became null Because you upcoming date from event that is different format and you are first setting format is different.
dateFormatter.dateFormat = #"MMMM dd yyyy";
that have to:
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ssZ";
You have to set first current date format then you can change it your want date.
Your setting an incorrect format for the date,
Try the below, this will work
//"myDate" variable is an example string, you can change it with "[events objectForKey:#"start_time"]"
NSString *myDate = #"2000-12-11T00:00:00 +0000";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init] init];
dateFormatter.dateFormat = #"yyyy-dd-mm'T'HH:mm:ss Z";
NSDate *myDateObject = [dateFormatter dateFromString:myDate]; //Now you have the date object that return the correct value
//FOR date
[dateFormatter setDateFormat:#"yyyy-dd-mm"];
NSString *OnlyDate = [dateFormatter stringFromDate:myDateObject];
//For Time
[dateFormatter setDateFormat:#"HH:mm:ss Z"];
NSString *OnlyTime = [dateFormatter stringFromDate:myDateObject];
NSLog(#"Date is %# and Time is %#",OnlyDate,OnlyTime);
UPDATE
In your case, if [events objectForKey:#"start_time"] is a NSDATE object then try the following
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init] init];
dateFormatter.dateFormat = #"yyyy-dd-mm'T'HH:mm:ss Z";
NSDate *myDateObject = [dateFormatter dateFromString:[events objectForKey:#"start_time"]]; //Now you have the date object that return the correct value
//FOR date
[dateFormatter setDateFormat:#"yyyy-dd-mm"];
NSString *OnlyDate = [dateFormatter stringFromDate:myDateObject];
//For Time
[dateFormatter setDateFormat:#"HH:mm:ss Z"];
NSString *OnlyTime = [dateFormatter stringFromDate:myDateObject];
NSLog(#"Date is %# and Time is %#",OnlyDate,OnlyTime);
Hope this helps.
i have a string like 2013-03-05T07:37:26.853 and i want to get the Output : 2013-03-05 07:37:26. I want the final output in NSDate object. i am using below function to convert desire output. and it returning wrong time.
- (NSDate *) getDateFromCustomString:(NSString *) dateStr
{ NSArray *dateStrParts = [dateStr componentsSeparatedByString:#"T"];
NSString *datePart = [dateStrParts objectAtIndex:0];
NSString *timePart = [dateStrParts objectAtIndex:1];
NSArray *dateParts = [datePart componentsSeparatedByString:#"-"];
NSArray *timeParts = [timePart componentsSeparatedByString:#":"];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setHour:[[timeParts objectAtIndex:0] intValue]];
[components setMinute:[[timeParts objectAtIndex:1] intValue]];
[components setSecond:[[timeParts objectAtIndex:2] intValue]];
[components setYear:[[dateParts objectAtIndex:0] intValue]];
[components setMonth:[[dateParts objectAtIndex:1] intValue]];
[components setDay:[[dateParts objectAtIndex:2] intValue]];
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDate *date = [currentCalendar dateFromComponents:components];
NSLog(#"Date 1:%#",date); // Returns wrong time (2013-03-05 02:07:26)
/* i had tried the below.
NSDateFormatter * format = [[NSDateFormatter alloc] init];
[format setTimeZone:NSTimeZoneNameStyleStandard];
[format setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSLog(#"Date 2:%#",[format stringFromDate:date]); // Returns correct date (2013-03-05 07:37:26)
NSDate *fDate = [format dateFromString:[format stringFromDate:date]];
DLog(#"Date 3:%#",fDate); // Returns wrong time (2013-03-05 02:07:26)
*/
[components release];
return date;
}
Plz suggest me if any idea.
What you're seeing here is a time zone issue. If you want to NSLog a correct looking date, we'll need to give the input string a GMT time zone:
Add this in your code and see what happens:
[components setTimeZone: [NSTimeZone timeZoneForSecondsFromGMT: 0]];
Try this,
NSString *departTimeDate = #"2013-03-05T07:37:26.853";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS"];
NSDate *date = [dateFormatter dateFromString:departTimeDate];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSLog(#"Expected Result___ %#",[dateFormatter stringFromDate:date]); //2013-03-05 07:37:26
Try this : issue is Time zone.
+ (NSDate *) getDate:(NSString *) dateStr
{
NSArray *dateStrParts = [dateStr componentsSeparatedByString:#"T"];
NSString *datePart = [dateStrParts objectAtIndex:0];
NSString *timePart = [dateStrParts objectAtIndex:1];
NSString *t = [[timePart componentsSeparatedByString:#"."] objectAtIndex:0];
NSString *newDateStr = [NSString stringWithFormat:#"%# %#",datePart,t];
//2013-03-05 07:37:26
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [df dateFromString:newDateStr];
DLog(#"%#",date);
return date;
}
NSString *dateString = [[items objectAtIndex:indexPath.row] objectForKey:#"date"];
NSLog(#"DATE: %#",dateString);
// outputs 16/10 23:59:49
NSDateFormatter *dateFormat;
[dateFormat setDateFormat:#"dd/MM HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateString];
NSLog(#"DATE: %#", date);
// outputs null
but it outputs null every time.
please can you help me what am i doing wrong?
You forgot to create NSDateFormatter:
You should replace line:
NSDateFormatter *dateFormat;
With:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
You need create
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
You have to set year otherwise date will be shown null. You can use current year and separated your string into months and days then set like this "dd-MM-yyyy" or any another way which you want.
NSString *string = #"16/10 23:59:49";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd"];
NSArray *arrayDateData = [string componentsSeparatedByString:#"/"];
NSString *dayStr =[arrayDateData objectAtIndex:0];
NSString *string1 =[arrayDateData objectAtIndex:1];
NSString *monStr = [[string1 componentsSeparatedByString:#" "] objectAtIndex:0];
NSString *timeString;
timeString=[NSString stringWithFormat:#"%d-%#-%#",2012,monStr,dayStr];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString=[df dateFromString:timeString];
NSLog(#"date from string: %#",dateFromString);
I think it will be helpful to you.