Getting date and time from unix timestamp - ios

I am trying to get current date and time from a unix time stamp but it keeps giving me the wrong date this is the code i am currenty
func getDateFromStamp(timeInterval:Int) -> String{
let date = NSDate(timeIntervalSince1970: TimeInterval(timeInterval))
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, MMM d, h:mm a"
let dateString = dateFormatter.string(from: date as Date)
return dateString
}

Usually unix timestamps are in milliseconds, but Date uses seconds. You might need to divide your timestamp by 1000.

Possible errors:
I try to use milliseconds instead of seconds. timeIntervalSince1970: TimeInterval uses seconds. Divide timeInterval by 1000 beforehand.
Or you have not setup TimeZone.
Don't forget to use Date() instead of NSDate()
It depends on your results and input parameters.
EDIT
To change time zone use this code:
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+01:00")

For Swift 4:
Step:-1 Convert to Date:
let timestamp = "Your TimeStamp here"
var dateFromTimeStamp = Date(timeIntervalSince1970: timestamp as! TimeInterval / 1000)
Step:-2 Convert to appropriate time zone:
let dateFormatter = DateFormatter();
let dateFormat = "dd MMM yyyy"; //Any TimeFormat you want
dateFormatter.dateFormat = dateFormat;
let formattedDate = dateFormatter.string(from: dateFromTimeStamp);
dateFormatter.locale = NSLocale.current;
dateFormatter.timeZone = TimeZone(abbreviation: "GMT"); //Pass Appropriate time zone here.
dateFormatter.dateFormat = dateFormat as String;
let sourceDate = dateFormatter.date(from: formattedDate as String);
print(sourceDate)
Hope so this helps You.

Related

Ios Swift Date Convert from TimeZone

dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "KST")
let nowstring = dateFormatter.string(from: Date())
let nowdate = dateFormatter.date(from: nowstring)
print(nowstring)
print(nowdate)
print this 2018-07-24T15:06:26
Optional(2018-07-24 06:06:26 +0000)
i want nowdate value 2018-07-24T15:06:26
but String to Date without timezone
I have to use to timeIntervalSince so nowdate is must have timezone
let today = NSDate()
print("\(today)")
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let nowDatestring = formatter.string(from: today)
formatter.timeZone = TimeZone(abbreviation: "KST")
let kstDate = formatter.date(from: nowDatestring)
print("\(kstDate!)")
formatter.timeZone = TimeZone(abbreviation: "PST")
let pstDate = formatter.date(from: nowDatestring)
print("\(pstDate!)")
formatter.timeZone = TimeZone(abbreviation: "IST")
let istDate = formatter.date(from: nowDatestring)
print("\(istDate!)")
Date is a specific point in time, which is independent of time zone. If you print a date value it will be always independent of any calendar or time zone. It will give same value for you(KST) and me(IST). You can specify your time zone when you format it using DateFormatter.

Using Date() in a 12-Hour Time Device

Was trying to use the current date with Date() and making some operation on it.
print("Date() -> ", Date())
When the device is on a 24-Hour Time mode:
Date() -> 2018-07-10 09:06:38 +0000
When the device is on a 12-Hour Time mode:
Date() -> 2018-07-10 99:04:09 AM +0000
Is this normal to have 99:04:09 for the 12-Hour Time mode? If yes, how do you manage your operations depending on the Hour Time mode?
Try to convert Date() into string with the below code and check what you are getting in 12hour date formate.
You can convert Date() into a device's date formate as per below code.
let formatter = DateFormatter()
formatter.timeZone = TimeZone.current
formatter.dateStyle = .short
formatter.timeStyle = .short
let dateString = formatter.string(from: Date())// it will print date in device's current date formate style.
For more reference on dateStyle/timeStyle refer this answer - https://stackoverflow.com/a/46668669/3705770
use this code
let formatter = DateFormatter()
// initially set the format based on your datepicker date
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let myString = formatter.string(from: myDate)
// convert your string to date
let yourDate = formatter.date(from: myString)
//then again set the date format whhich type of output you need
formatter.dateFormat = "dd-MMM-yyyy"
// again convert your date to string
let myStringafd = formatter.string(from: yourDate!)

How to Convert date into local time zone ios swift [duplicate]

This question already has answers here:
Converting UTC date format to local nsdate
(7 answers)
Closed 5 years ago.
I'm working on date formatter, I got a response of date from server in string type, which I convert into date format but what I want to do is to convert a date and then manage according to local time.
For example, if 12/06/2017, 06:48:03 is a date from server and i'm from Pakistan so it gives me a date and time according to GMT+5 which is 12/06/2017, 11:48:03
Same as from India it gives me a date and time according to GMT+5:30 which is 12/06/2017, 12:18:03
Here is a source code
public class func converServerTimeStampToDate (_ timeStamp: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy, hh:mm:ss a"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let localDate = dateFormatter.date(from: timeStamp)
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = "MM/dd/yyyy, hh:mm:ss a"
// return dateFormatter.string(from: localDate!)
return dateFormatter.date(from:dateFormatter.string(from:
localDate!))!
}
Any help would be appreciated !!
If you want the result to be a Date object just use the first part of #Intellij-Shivam's answer:
func serverToLocal(date:String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let localDate = dateFormatter.date(from: date)
return localDate
}
(note that DateFormatter.date(from:) returns an optional, which is correct because the input date string might not be in the correct format.)
There is no such thing as a Date in your local time zone. Dates don't have a time zone. They record an instant in time all over the planet.
To display a date in your local time zone you can use the DateFormatter class method localizedString():
let dateString = DateFormatter.localizedString(
inputDate,
dateStyle: .medium,
timeStyle: .medium)
I hope this will work for your problem
func serverToLocal(date:String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let date = dateFormatter.date(from: date)
dateFormatter.timeZone = TimeZone.current
let timeStamp = dateFormatter.string(from: date!)
return date
}

Can't find the right date formatter (DateFormatter) in iphone

I am getting date string from server like that "2017-08-05T00:30:00.000+02:00". Below is my code for date formatter.
let dateFormatter: DateFormatter = DateFormatter()
let serverDateString = "2017-08-05T00:30:00.000+02:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZZ"
dateFormatter.locale = Foundation.Locale(identifier: "en_US_POSIX")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
But it returns 2017-08-04 22:30:00 +0000. Which is wrong. Any suggestions?
In Swift 3
You can change your server time string to UTC time Date as:
let dateFormatter: DateFormatter = DateFormatter()
var serverDateString = "2017-08-05T00:30:00.000+02:00"
let index = serverDateString.index(serverDateString.startIndex, offsetBy: 19)
serverDateString = serverDateString.substring(to: index)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone.init(identifier: "UTC")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
Your Code for parsing date is correct: 2017-08-05T00:30:00.000+02:00 and 2017-08-04 22:30:00 +0000 represent the same time, just in different time zones. Your only problem is that Date doesn't actually store the time zone
yyyy-MM-dd'T'hh:mm:ss.SSSZZZZZZ
This is correct date formate for your input string. Here is the Apple Document for more description.
Here is my code:
var strInputDateString: String = "2017-08-05T00:30:00.000+02:00"
var dateFormat = DateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd'T'hh:mm:ss.SSSZZZZZZ"
//Set new dateFormate
var date1: Date? = dateFormat.date(from: strInputDateString)
dateFormat.dateFormat = "dd-MM-YYYY hh:mm:ss"
// Your Desire
var strOutputDateString: String = dateFormat.string(from: date1!)
print("\(strInputDateString)")
print("\(strOutputDateString)")
output:
Main input String:2017-08-05T00:30:00.000+02:00
Converted String: 05-08-2017 04:00:00
For now, I tried to convert your date format with my custom. It's give me the perfect output.
Actually, your code is good.
I copied your data and ran it in playground.
What kind of format you want it be?
Try this
let dateFormatter: DateFormatter = DateFormatter()
let serverDateString = "2017-08-05T00:30:00.000+02:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"
dateFormatter.locale = Foundation.Locale(identifier: "en_US_POSIX")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
Hope this helps
The step you are missing is to again use your DateFormatter to format the date into a string. When you use print on the date, it will always show GMT. For illustration purposes, I've set the TimeZone to Berlin to match the original offset of +2:00.
let dateFormatter: DateFormatter = DateFormatter()
let serverDateString = "2017-08-05T00:30:00.000+02:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZZ"
// Hard set to Berlin
dateFormatter.timeZone = TimeZone(identifier: "Europe/Berlin")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
// Your missing step
dateFormatter.string(from: currentDate)
Remember that DateFormatters are about:
Parsing from a String - df.date(from: String)
Formatting from a Date - df.string(from: Date)

How to convert string 24hrs type time into 12hrs time

From response I have set time in String, its contains HH: mm format the task is I have to convert that string into 12hrs time interval with AM and PM in another string to display results
For example "16:36:00.000Z" I want my result like 4:36 PM
Here my sample code:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = dateFormatter.dateFromString(dateValue)
// To convert the date into an HH:mm format
dateFormatter.dateFormat = "HH:mm"
let dateString = dateFormatter.stringFromDate(date!)
print(dateString)
But unfortunately I'm getting nil in date
do like
let dateFormatter = NSDateFormatter()
16:36:00.000Z
dateFormatter.dateFormat = "HH:mm:ss.sssZ"
let date = dateFormatter.dateFromString(dateValue)
// To convert the date into an HH:mm format
dateFormatter.dateFormat = "hh:mm a" // or //h:mm a
let dateString = dateFormatter.stringFromDate(date!)
print(dateString)

Resources