iOS Revisited: Date

Hot

Post Top Ad

Showing posts with label Date. Show all posts
Showing posts with label Date. Show all posts

12 Oct 2017

Convert One Date Format To Another Date Format Swift 4 - iOS

10/12/2017 03:07:00 am 0
Converting a date string from one date format to another date format is an easy by using the following method:

Convert One Date Format To Another Date Format Swift 4 - iOS


Date Formats:

Following are the some Date Formats:

Thursday, Oct 12, 2017 - EEEE, MMM d, yyyy
10/12/2017 - MM/dd/yyyy
10-12-2017 09:48 - MM-dd-yyyy HH:mm
Oct 12, 9:48 AM - MMM d, h:mm a
October 2017 - MMMM yyyy
Oct 12, 2017 - MMM d, yyyy
Thu, 12 Oct 2017 09:48:59 +0000 - E, d MMM yyyy HH:mm:ss Z
2017-10-12T09:48:59+0000 - yyyy-MM-dd'T'HH:mm:ssZ
12.10.17 - dd.MM.yy

For more check here.

One Date Format To Another:

For converting we are going to create String extension as follow:
extension String
{
    func toDateString( inputDateFormat inputFormat  : String,  ouputDateFormat outputFormat  : String ) -> String
    {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = inputFormat
        let date = dateFormatter.date(from: self)
        dateFormatter.dateFormat = outputFormat
        return dateFormatter.string(from: date!)
    }
}

Usage:

Add the following lines of code for usage:
let dateString = "2017-10-10 15:56:25"
let requiredFormat = dateString.toDateString(inputDateFormat: "yyyy-MM-dd HH:mm:ss", ouputDateFormat: "dd 'at' hh:mm:ss")
print(requiredFormat)

//output will be '10 at 03:56:25'

The inputDateFormat should be same as dateString, but the ouputDateFormat can be different as we required.
Read More

11 Oct 2017

Convert Date To String & Vice-Versa Swift 4 - iOS

10/11/2017 05:54:00 am 0
Convert Date To String & Vice-Versa Swift 4 - iOS

Date To String:


For converting Date to String we are going to create extension for Date as follow:
extension Date
{
    func toString( dateFormat format  : String ) -> String
    {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        return dateFormatter.string(from: self)
    }
}

In above method we can set the required output date format.

Usage:

Add the following lines of code for usage:

let dateString = Date().toString(dateFormat: "yyyy/MMM/dd HH:mm:ss")
print("dateString is \(dateString)")

// output will be 'dateString is 2017/Oct/11 17:16:23'

String To Date:


For converting string to date, we have to give the date format as the string.

Add the following extension to String:
extension String
{
    func toDate( dateFormat format  : String) -> Date
    {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
        return dateFormatter.date(from: self)!
    }
    
}

Usage:

Add the following lines of code for usage:
let dateString = "2017-10-10 15:56:25"
let date = dateString.toDate(dateFormat: "yyyy-MM-dd HH:mm:ss")
print("Date is \(date)")

// output is 'Date is 2017-10-10 15:56:25 +0000'
Read More

10 Oct 2017

Convert UTC Timezone To Local/Device Current Timezone and Vice-Versa Swift 4 - iOS

10/10/2017 11:01:00 am 0
In this article we are going to convert UTC Date format to Current device date format.

UTC is the time standard commonly used across the world. The world's timing centers have agreed to keep their time scales closely synchronized - or coordinated - therefore the name Coordinated Universal Time.

Convert UTC Timezone To Local/Device Current Timezone and Vice-Versa Swift 4 - iOS

I know that you guys are totally confused of this date Timezone conversions.

Here we simply use following method to convert UTC to local.

UTC to Local:

Use the following method for converting:
func UTCToLocal(UTCDateString: String) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" //Input Format
    dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
    let UTCDate = dateFormatter.date(from: UTCDateString)
    dateFormatter.dateFormat = "yyyy-MMM-dd hh:mm:ss" // Output Format
    dateFormatter.timeZone = TimeZone.current
    let UTCToCurrentFormat = dateFormatter.string(from: UTCDate!)
    return UTCToCurrentFormat
}

Usage:

Call above method as follow:
let dateString = "2017-10-10 9:56:25"

let date = self.UTCToLocal(UTCDateString: dateString)

//output date should be '2017-10-10 15:26:25'

In above method we can customize date formats as per our usage. But the input date format must be same format as input date string.

Local to UTC:

Use the following method for converting:
func localToUTC(date:String) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    dateFormatter.calendar = NSCalendar.current
    dateFormatter.timeZone = TimeZone.current
    
    let dt = dateFormatter.date(from: date)
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
    dateFormatter.dateFormat = "yyyy-MMM-dd hh:mm:ss"
    
    return dateFormatter.string(from: dt!)
}

Usage:

Call above method as follow:
let dateString = "2017-10-10 15:56:25"

let date = self.localToUTC(date: dateString)

//output date should be '2017-10-10 09:26:25'

Read More

Post Top Ad