为什么我的 NSDate 格式不正确?
Why is my NSDate not being formatted correctly?
我有一个 NSDate
格式不正确。
我在 .h 中声明了一个 UITextField
:
@property (weak, nonatomic) IBOutlet UITextField *datetimeTextField;
然后我有一个第 3 方 UIPicker,它选择一个日期并将其插入到提到的 TextField 中:
// Method to avoid diplaying the keyboard.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
ActionSheetDatePicker *datePicker = [[ActionSheetDatePicker alloc]initWithTitle:@"Select Date and Time" datePickerMode:UIDatePickerModeDateAndTime selectedDate:[NSDate date] doneBlock:^(ActionSheetDatePicker *picker, id selectedDate, id origin) {
// As you can see here it's taking the correct (non-formatted) date
NSLog(@"Selected %@", selectedDate); // VALUE = Sat Nov 10 10:00:41 2018
//create a date formatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//set its format as the required output format
[formatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
//get the output date as string
NSString *selectedDateString = [formatter stringFromDate:selectedDate];
self.datetimeTextField.text = selectedDateString;
// And here I get the value I want to store in Parse stored in datetimeTextField.text
NSLog(@"Selected After reformat %@", self.datetimeTextField.text); // VALUE = 10-11-2018 00:35:06
} cancelBlock:^(ActionSheetDatePicker *picker) {
} origin:self.view];
datePicker.minuteInterval = 5;
[datePicker showActionSheetPicker];
return NO;
}
当我必须调用 IBAction 将此 NSDate 存储在我的 Parse Cloud 中时,我的问题就开始了(我有一个只接受 NSDate
.
的日期列
- (IBAction)createeventAction:(id)sender{
// Here I "catch" the value previously stored from the Picker.
NSString *dateString = datetimeTextField.text; //// 07-11-2018 22:00:42 (correct format)
// Here I convert the NSString into NSDate with the same formatting
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateString];
// But for some reason, date prints incorrectly.
NSLog(@"DATE in here ====>>> %@", date); // Sat Nov 10 10:00:41 2018
}
问题:
我想在不丢失格式的情况下将 NSString (datetimeTextField.text) 转换为 NSDate。
编辑 1:
我参考了这个问题 How to convert NSStrings to NSDate 的公认答案,但出于某种原因,它对我不起作用。
编辑 2:
为了更清楚:
将 NSDate 转换为 NSString 的代码。
// We have a date (not formatted) => Sat Nov 10 10:00:41 2018
//create a date formatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
NSString *selectedDateString = [formatter stringFromDate:selectedDate];
self.datetimeTextField.text = selectedDateString;
// Date formatted => 10-11-2018 00:35:06
将 NSString 转换回 NSDate 的代码:
NSString *dateString = datetimeTextField.text; // 10-11-2018 10:00:41 (correct format)
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(@"DATE in here ====>>> %@", date); // Sat Nov 10 10:00:41 2018 (not formatted. WHY?)
Well thats the thing. I need to have a NSDate because I am storing dates.
您似乎误解了 NSDate
和从 NSDateFormatter
返回的内容之间的区别。 NSDate
只是一个 class,它以不特定的 格式 存储日期——它将信息存储在对象内部。如果要以特定格式显示日期,则需要从日期创建一个 string,然后使用 NSDateFormatter
将日期转换为表示您需要的格式的日期。
如果您使用 NSLog()
将日期打印到控制台,例如:
NSLog("My date is %@", myDate);
然后 NSLog
将只使用日期的 description
方法,它为您提供了一种日期的默认表达式。如果您想以某种特定格式记录日期,则需要使用该格式设置日期格式化程序,然后使用它:
NSLog("My formatted date is %@", [myFormatter stringFromDate:myDate]);
最后证明我的代码没问题。唯一的问题是我没有注意到 Date 在 Parse 中被声明为 String 而不是 Date。
我有一个 NSDate
格式不正确。
我在 .h 中声明了一个 UITextField
:
@property (weak, nonatomic) IBOutlet UITextField *datetimeTextField;
然后我有一个第 3 方 UIPicker,它选择一个日期并将其插入到提到的 TextField 中:
// Method to avoid diplaying the keyboard.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
ActionSheetDatePicker *datePicker = [[ActionSheetDatePicker alloc]initWithTitle:@"Select Date and Time" datePickerMode:UIDatePickerModeDateAndTime selectedDate:[NSDate date] doneBlock:^(ActionSheetDatePicker *picker, id selectedDate, id origin) {
// As you can see here it's taking the correct (non-formatted) date
NSLog(@"Selected %@", selectedDate); // VALUE = Sat Nov 10 10:00:41 2018
//create a date formatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//set its format as the required output format
[formatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
//get the output date as string
NSString *selectedDateString = [formatter stringFromDate:selectedDate];
self.datetimeTextField.text = selectedDateString;
// And here I get the value I want to store in Parse stored in datetimeTextField.text
NSLog(@"Selected After reformat %@", self.datetimeTextField.text); // VALUE = 10-11-2018 00:35:06
} cancelBlock:^(ActionSheetDatePicker *picker) {
} origin:self.view];
datePicker.minuteInterval = 5;
[datePicker showActionSheetPicker];
return NO;
}
当我必须调用 IBAction 将此 NSDate 存储在我的 Parse Cloud 中时,我的问题就开始了(我有一个只接受 NSDate
.
- (IBAction)createeventAction:(id)sender{
// Here I "catch" the value previously stored from the Picker.
NSString *dateString = datetimeTextField.text; //// 07-11-2018 22:00:42 (correct format)
// Here I convert the NSString into NSDate with the same formatting
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateString];
// But for some reason, date prints incorrectly.
NSLog(@"DATE in here ====>>> %@", date); // Sat Nov 10 10:00:41 2018
}
问题:
我想在不丢失格式的情况下将 NSString (datetimeTextField.text) 转换为 NSDate。
编辑 1:
我参考了这个问题 How to convert NSStrings to NSDate 的公认答案,但出于某种原因,它对我不起作用。
编辑 2:
为了更清楚:
将 NSDate 转换为 NSString 的代码。
// We have a date (not formatted) => Sat Nov 10 10:00:41 2018
//create a date formatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
NSString *selectedDateString = [formatter stringFromDate:selectedDate];
self.datetimeTextField.text = selectedDateString;
// Date formatted => 10-11-2018 00:35:06
将 NSString 转换回 NSDate 的代码:
NSString *dateString = datetimeTextField.text; // 10-11-2018 10:00:41 (correct format)
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(@"DATE in here ====>>> %@", date); // Sat Nov 10 10:00:41 2018 (not formatted. WHY?)
Well thats the thing. I need to have a NSDate because I am storing dates.
您似乎误解了 NSDate
和从 NSDateFormatter
返回的内容之间的区别。 NSDate
只是一个 class,它以不特定的 格式 存储日期——它将信息存储在对象内部。如果要以特定格式显示日期,则需要从日期创建一个 string,然后使用 NSDateFormatter
将日期转换为表示您需要的格式的日期。
如果您使用 NSLog()
将日期打印到控制台,例如:
NSLog("My date is %@", myDate);
然后 NSLog
将只使用日期的 description
方法,它为您提供了一种日期的默认表达式。如果您想以某种特定格式记录日期,则需要使用该格式设置日期格式化程序,然后使用它:
NSLog("My formatted date is %@", [myFormatter stringFromDate:myDate]);
最后证明我的代码没问题。唯一的问题是我没有注意到 Date 在 Parse 中被声明为 String 而不是 Date。