Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

iOS/iPhone DateDiff / TimeDiff / Convert Date (NSString to NSDate, NSDate to NSString)

iOS/iPhone DateDiff / TimeDiff / Convert Date (NSString to NSDate, NSDate to NSString) ในการเขียน App โปรแกรมด้วยภาษาต่าง ๆ หลาย ๆ ครั้งเราอาจจะต้องใช้พวกเปรียบเทียบวันที่ Date Diff หรือ เปรียบเทียบเวลา Time Diff รวมทั้งการแปลง String ให้เป็น Date และ Date ให้เป็น String ต่าง ๆ และในบทความนี้ จะเขียนตัวอย่างการใช้ Function NSDate กับ NSString แบบง่าย ๆ ที่จะสามารถนำไปประยุกต์ใช้กับโปรแกรมได้

iOS/iPhone DateDiff / TimeDiff / Convert Date (NSString to NSDate, NSDate to NSString)

iOS/iPhone DateDiff / TimeDiff / Convert Date (NSString to NSDate, NSDate to NSString)


ใน Objective-C ให้ความสำคัญกับ Data Type ค่อนข้างจะยาก เพราะฉะนั้นการนำตัวแปรไปทำอะไรต่าง ๆ ก็จะต้องให้อยู่ในรูปแบบที่ถูกต้อง เช่น NSDate จะไม่สามารถนำไป Assign ค่าให้กับ Label ได้โดยตรง แต่จะต้องแปลงให้อยู่ในรูปแบบ NSString เสียก่อน

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
    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;

NSString to NSDate
    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;

NSDate to NSString
    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;









Example การใช้งาน DateDiff / TimeDiff / Convert Date แบบง่าย ๆ

iOS/iPhone DateDiff / TimeDiff / Convert Date (NSString to NSDate, NSDate to NSString)

เริ่มต้นด้วยการสร้าง Application แบบ Single View Application แบบง่าย ๆ

iOS/iPhone DateDiff / TimeDiff / Convert Date (NSString to NSDate, NSDate to NSString)

เลือกและไม่เลือกดังรูป

iOS/iPhone DateDiff / TimeDiff / Convert Date (NSString to NSDate, NSDate to NSString)

ตอนนี้หน้าจอ View ยังว่าง ๆ

iOS/iPhone DateDiff / TimeDiff / Convert Date (NSString to NSDate, NSDate to NSString)

ออกแบบหน้าจอ View ด้วย Label ต่าง ๆ ดังรูป

iOS/iPhone DateDiff / TimeDiff / Convert Date (NSString to NSDate, NSDate to NSString)

ใน Class ของ .h ให้ทำการเชื่อม IBOutlet ดังรูป จากนั้นจะเป็นส่วนของ Code ของ Objective-C

ViewController.h
//
//  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
//
//  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


Screenshot

iOS/iPhone DateDiff / TimeDiff / Convert Date (NSString to NSDate, NSDate to NSString)

แสดงการใช้ DateDiff / TimeDiff และ Convert Date (NSString to NSDate, NSDate to NSString) แบบง่าย ๆ

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-12-12 14:10:06 / 2017-03-25 22:45:30
  Download : Download  iOS/iPhone DateDiff / TimeDiff / Convert Date (NSString to NSDate, NSDate to NSString)
 Sponsored Links / Related

 
iOS/iPhone Date Time and Date Format (NSDate, Objective-C)
Rating :

 
iOS/iPhone Date Picker (UIDatePicker) Example (iPhone,iPad)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่