簡體   English   中英

在xcode中以MM / dd / yyyy格式轉換日期?

[英]Convert date in MM/dd/yyyy format in xcode?

我有一個UIDatePicker ,我以yyyy-MM-dd格式從中獲取所選日期。現在我想將其轉換為MM/dd/yyyy格式..我怎么能這樣做?

NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease];
df.dateFormat = @"yyyy-MM-dd";
NSArray *temp=[[NSString stringWithFormat:@"%@",[df stringFromDate:DatePicker.date]] componentsSeparatedByString:@""];

[dateString2 release];
dateString2=nil;
dateString2= [[NSString alloc]initWithString:[temp objectAtIndex:0]];
NSLog(@"%@",dateString2);

我得到2012-10-30,但我想要10/30/2012。

請參閱每行的說明或介紹

NSString *str = @"2012-10-30"; /// here this is your date with format yyyy-MM-dd

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:@"yyyy-MM-dd"]; //// here set format of date which is in your output date (means above str with format)

NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format

dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];// here set format which you want...

NSString *convertedString = [dateFormatter stringFromDate:date]; //here convert date in NSString
NSLog(@"Converted String : %@",convertedString);

df.dateFormat = @“MM / dd / yyyy”;

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MM/dd/yyyy"];

只是為了看看它是如何結束的:

NSDateFormatter* df = [[[NSDateFormatter alloc]init] autorelease];
df.dateFormat = @"MM/dd/yyyy";
NSString* dateString = [df stringFromDate:datePicker.date];  // Don't use leading caps on variables
NSLog(@"%@", dateString);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];    
[formatter setDateFormat:@"MM/dd/yyyy"];    

原來接受的答案會做一些額外的事情:

1)它分配兩個不需要的NSDateFormatter實例。 NSDateFormatter的創建成本很高。

2)最好在完成日期格式化時明確發布日期格式化程序而不是使用自動釋放的延遲發布。

//get datepicker date (NSDate *)
NSDate *datePickerDate = [myDatePicker date];

//create a date formatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 

//set its format as the required output format
[formatter setDateFormat:@"MM/dd/yyyy"];

//get the output date as string
NSString *outputDateString = [formatter stringFromDate:datePickerDate];

//release formatter
[formatter release];
- (void)showTime {
   NSDate *now = [NSDate date];
   NSDateFormatter *dateFormatter = [NSDateFormatter new];
   [dateFormatter setDateFormat:@"HH:mm:ss"];
   // you label
   _time.text = [dateFormatter stringFromDate:now]; 
}

- (void)showDate {
   NSDate *now = [NSDate date];
   NSDateFormatter *dateFormatter = [NSDateFormatter new];
   [dateFormatter setDateFormat:@"MM/dd/yyyy"];
   // you label
   _date.text = [dateFormatter stringFromDate:now];
}

- (void)viewDidLoad {
   [super viewDidLoad];
    self.showTime;
    self.showDate;
}

請享用

NSString *str = @"2012-10-30"; /// here this is your date with format yyyy-MM-dd

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:@"yyyy-MM-dd"]; //// here set format of date which is in your output date (means above str with format)

NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format

dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];// here set format which you want...

NSString *convertedString = [dateFormatter stringFromDate:date]; here convert date in NSString
NSLog(@"Converted String : %@",convertedString);

我使用帶有NSISO8601DateFormatWithDashSeparatorInDate選項的代碼:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString * stringformat = [NSDateFormatter dateFormatFromTemplate:@"MMddy" options:NSISO8601DateFormatWithDashSeparatorInDate locale:[NSLocale systemLocale]];
[dateFormatter setDateFormat:stringformat];

NSDate *d = [NSDate date]; // or set your date
NSLog(@"date in format %@",  [dateFormatter stringFromDate:d]); // date in format 2018-06-20

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM