NSString *string1 = @"2012-12-05"; NSString *string2 = @"2012-12-09"; // Date Format NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init]; [dateFormater setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Bangkok"]]; [dateFormater setDateFormat:@"YYYY-MM-dd"]; NSDate *datetime1 = [dateFormater dateFromString:string1]; NSDate *datetime2 = [dateFormater dateFromString:string2]; double dateInterval = [datetime2 timeIntervalSinceDate:datetime1] / (60*60*24); NSMutableString *resultDateDiff = [NSString stringWithFormat:@"DateDiff = %.0f Days",dateInterval]; lblDateDiff.text = resultDateDiff;
NSString *string1 = @"2012-12-09 15:45:00"; NSString *string2 = @"2012-12-09 15:50:00"; // Date Format NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init]; [dateFormater setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Bangkok"]]; [dateFormater setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; NSDate *datetime1 = [dateFormater dateFromString:string1]; NSDate *datetime2 = [dateFormater dateFromString:string2]; double timeInterval = [datetime2 timeIntervalSinceDate:datetime1] / (60); NSMutableString *resultTimeDiff = [NSString stringWithFormat:@"TimeDiff = %.2f Minutes",timeInterval]; lblTimeDiff.text = resultTimeDiff;
NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init]; [dateFormater setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // NSString to NSDate NSString *dateString = @"2012-12-09 15:45:00"; NSDate *dateFromString = [[NSDate alloc] init]; dateFromString = [dateFormater dateFromString:dateString]; // NSDate to NSString NSString *strDate = [dateFormater stringFromDate:[NSDate date]]; lblConvertDate.text = strDate;
// // ViewController.h // DateDiffTimeDiff // // Created by Weerachai on 12/8/55 BE. // Copyright (c) 2555 Weerachai. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController { IBOutlet UILabel *lblDateDiff; IBOutlet UILabel *lblTimeDiff; IBOutlet UILabel *lblConvertDate; } @end
// // ViewController.m // DateDiffTimeDiff // // Created by Weerachai on 12/8/55 BE. // Copyright (c) 2555 Weerachai. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.DateDiff; self.TimeDiff; self.ConvertDate; } // DateDiff - (void)DateDiff { NSString *string1 = @"2012-12-05"; NSString *string2 = @"2012-12-09"; // Date Format NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init]; [dateFormater setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Bangkok"]]; [dateFormater setDateFormat:@"YYYY-MM-dd"]; NSDate *datetime1 = [dateFormater dateFromString:string1]; NSDate *datetime2 = [dateFormater dateFromString:string2]; double dateInterval = [datetime2 timeIntervalSinceDate:datetime1] / (60*60*24); NSMutableString *resultDateDiff = [NSString stringWithFormat:@"DateDiff = %.0f Days",dateInterval]; lblDateDiff.text = resultDateDiff; } // TimeDiff - (void)TimeDiff { NSString *string1 = @"2012-12-09 15:45:00"; NSString *string2 = @"2012-12-09 15:50:00"; // Date Format NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init]; [dateFormater setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Bangkok"]]; [dateFormater setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; NSDate *datetime1 = [dateFormater dateFromString:string1]; NSDate *datetime2 = [dateFormater dateFromString:string2]; double timeInterval = [datetime2 timeIntervalSinceDate:datetime1] / (60); NSMutableString *resultTimeDiff = [NSString stringWithFormat:@"TimeDiff = %.2f Minutes",timeInterval]; lblTimeDiff.text = resultTimeDiff; } // Convert Date - (void)ConvertDate { NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init]; [dateFormater setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // NSString to NSDate NSString *dateString = @"2012-12-09 15:45:00"; NSDate *dateFromString = [[NSDate alloc] init]; dateFromString = [dateFormater dateFromString:dateString]; // NSDate to NSString NSString *strDate = [dateFormater stringFromDate:[NSDate date]]; lblConvertDate.text = strDate; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)dealloc { [lblDateDiff release]; [lblTimeDiff release]; [lblConvertDate release]; [super dealloc]; } @end
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท