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
Related
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);
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.
OK, I know I am missing something but I don't know what.
My NSDateFormmater comes up in the textField(personMonthdayTextField) with todays date but when I selected the datePicker and input it into the textField, it comes up with the standard format with the hours, minutes, seconds which I don't need.
Any help is appreciated.
Thanks
-(void)viewDidLoad {
NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MMM/yyyy"];
NSString* str = [formatter stringFromDate:date];
NSLog(#"%#",str);
NSMutableArray *Arr=[str componentsSeparatedByString:#" "];
personMonthdayTextField.text=[Arr objectAtIndex:0];
}
- (IBAction)done:(id)sender {
NSDate *dateSelected = [datepick date];
NSString *dateStamp = [[NSString alloc]initWithFormat:#"%#", dateSelected];
personMonthdayTextField.text = dateStamp;
}
This will give you date only
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
datelabel.text = [NSString stringWithFormat:#"%#",
[df stringFromDate:[NSDate date]]];
Adapt this for your code under your IBAction method
just use NSDateFormatter in your IBAction
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *dateSelected = [datepick date];
personMonthdayTextField.text = [dateFormatter stringFromDate:dateSelected];
you can also tweak with formats of date give in Apple ios sdk ,easily to get desired format.
If you only want the day why don't you set the dateFormat as "dd", it would save the need to separate the components of dateString.
-(void)viewDidLoad {
NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd"];
personMonthdayTextField.text = [formatter stringFromDate:date];
}
- (IBAction)done:(id)sender {
NSDate *dateSelected = [datepick date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd"];
personMonthdayTextField.text = [formatter stringFromDate: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.