How to convert Unix date from server to Date, and formatting it to timezone current
up vote
-3
down vote
favorite
I have fetched a Unix date string from the server, which I want to format to a Date with current timezone.
if let endDate = json["EndTime"].string {
self.endTime = self.convertDate(dateString: endDate as
NSString)
}
// dateString format = "/Date(1542600000000+0100)/"
func convertDate(dateString:NSString) -> Date{
let startIndex = dateString.range(of: "(").location + 1
let endIndex = dateString.range(of: ")").location
let range = NSRange(location: startIndex, length: endIndex-
startIndex)
let milliseconds = (dateString.substring(with: range) as
NSString).longLongValue
let interval = TimeInterval(milliseconds / 1000)
let date = Date(timeIntervalSince1970: interval)
return date
}
I successfully get the timeIntervalSince1970, but when starting to format the date it goes wrong. When formating it to string from date I get the correct datestring, but when I format string to date, the date is one hour behind. I am aware of that this probably has something to do with the timezone in some way, but I can’t get it right.
The expected output should be: 2018-11-19 17:00:00
Actual output is: 2018-11-19 16:00:00
The date should be formatted based on the timezone of the users device
Thanks for any help in advance!
ios swift date timezone
|
show 5 more comments
up vote
-3
down vote
favorite
I have fetched a Unix date string from the server, which I want to format to a Date with current timezone.
if let endDate = json["EndTime"].string {
self.endTime = self.convertDate(dateString: endDate as
NSString)
}
// dateString format = "/Date(1542600000000+0100)/"
func convertDate(dateString:NSString) -> Date{
let startIndex = dateString.range(of: "(").location + 1
let endIndex = dateString.range(of: ")").location
let range = NSRange(location: startIndex, length: endIndex-
startIndex)
let milliseconds = (dateString.substring(with: range) as
NSString).longLongValue
let interval = TimeInterval(milliseconds / 1000)
let date = Date(timeIntervalSince1970: interval)
return date
}
I successfully get the timeIntervalSince1970, but when starting to format the date it goes wrong. When formating it to string from date I get the correct datestring, but when I format string to date, the date is one hour behind. I am aware of that this probably has something to do with the timezone in some way, but I can’t get it right.
The expected output should be: 2018-11-19 17:00:00
Actual output is: 2018-11-19 16:00:00
The date should be formatted based on the timezone of the users device
Thanks for any help in advance!
ios swift date timezone
1
What’s the point of the date formatter? What’s the point of converting the Date to String to Date? And you need to provide specific details. Show the exact input, the exact output, and the desired output.
– rmaddy
Nov 22 at 15:38
I'm quite certain what you have is not an Unix timestamp
– mag_zbc
Nov 22 at 15:38
Please add an example ofdateString
. And don't useNSString
in Swift.
– vadian
Nov 22 at 15:39
This is a really poor question. As rmaddy says, you need to show an actual input date string and the desired output date string format, tell us the time zone you are converting to, and tell us how you conclude that the date is one hour off. (You say when you format the string to a date the hour is off, but why do you think that? ADate
object doesn't have a time zone, and the default logging will display it in UTC.)
– Duncan C
Nov 22 at 16:01
I have now edited the question now
– kMose
Nov 22 at 16:42
|
show 5 more comments
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
I have fetched a Unix date string from the server, which I want to format to a Date with current timezone.
if let endDate = json["EndTime"].string {
self.endTime = self.convertDate(dateString: endDate as
NSString)
}
// dateString format = "/Date(1542600000000+0100)/"
func convertDate(dateString:NSString) -> Date{
let startIndex = dateString.range(of: "(").location + 1
let endIndex = dateString.range(of: ")").location
let range = NSRange(location: startIndex, length: endIndex-
startIndex)
let milliseconds = (dateString.substring(with: range) as
NSString).longLongValue
let interval = TimeInterval(milliseconds / 1000)
let date = Date(timeIntervalSince1970: interval)
return date
}
I successfully get the timeIntervalSince1970, but when starting to format the date it goes wrong. When formating it to string from date I get the correct datestring, but when I format string to date, the date is one hour behind. I am aware of that this probably has something to do with the timezone in some way, but I can’t get it right.
The expected output should be: 2018-11-19 17:00:00
Actual output is: 2018-11-19 16:00:00
The date should be formatted based on the timezone of the users device
Thanks for any help in advance!
ios swift date timezone
I have fetched a Unix date string from the server, which I want to format to a Date with current timezone.
if let endDate = json["EndTime"].string {
self.endTime = self.convertDate(dateString: endDate as
NSString)
}
// dateString format = "/Date(1542600000000+0100)/"
func convertDate(dateString:NSString) -> Date{
let startIndex = dateString.range(of: "(").location + 1
let endIndex = dateString.range(of: ")").location
let range = NSRange(location: startIndex, length: endIndex-
startIndex)
let milliseconds = (dateString.substring(with: range) as
NSString).longLongValue
let interval = TimeInterval(milliseconds / 1000)
let date = Date(timeIntervalSince1970: interval)
return date
}
I successfully get the timeIntervalSince1970, but when starting to format the date it goes wrong. When formating it to string from date I get the correct datestring, but when I format string to date, the date is one hour behind. I am aware of that this probably has something to do with the timezone in some way, but I can’t get it right.
The expected output should be: 2018-11-19 17:00:00
Actual output is: 2018-11-19 16:00:00
The date should be formatted based on the timezone of the users device
Thanks for any help in advance!
ios swift date timezone
ios swift date timezone
edited Nov 22 at 18:33
asked Nov 22 at 15:33
kMose
317
317
1
What’s the point of the date formatter? What’s the point of converting the Date to String to Date? And you need to provide specific details. Show the exact input, the exact output, and the desired output.
– rmaddy
Nov 22 at 15:38
I'm quite certain what you have is not an Unix timestamp
– mag_zbc
Nov 22 at 15:38
Please add an example ofdateString
. And don't useNSString
in Swift.
– vadian
Nov 22 at 15:39
This is a really poor question. As rmaddy says, you need to show an actual input date string and the desired output date string format, tell us the time zone you are converting to, and tell us how you conclude that the date is one hour off. (You say when you format the string to a date the hour is off, but why do you think that? ADate
object doesn't have a time zone, and the default logging will display it in UTC.)
– Duncan C
Nov 22 at 16:01
I have now edited the question now
– kMose
Nov 22 at 16:42
|
show 5 more comments
1
What’s the point of the date formatter? What’s the point of converting the Date to String to Date? And you need to provide specific details. Show the exact input, the exact output, and the desired output.
– rmaddy
Nov 22 at 15:38
I'm quite certain what you have is not an Unix timestamp
– mag_zbc
Nov 22 at 15:38
Please add an example ofdateString
. And don't useNSString
in Swift.
– vadian
Nov 22 at 15:39
This is a really poor question. As rmaddy says, you need to show an actual input date string and the desired output date string format, tell us the time zone you are converting to, and tell us how you conclude that the date is one hour off. (You say when you format the string to a date the hour is off, but why do you think that? ADate
object doesn't have a time zone, and the default logging will display it in UTC.)
– Duncan C
Nov 22 at 16:01
I have now edited the question now
– kMose
Nov 22 at 16:42
1
1
What’s the point of the date formatter? What’s the point of converting the Date to String to Date? And you need to provide specific details. Show the exact input, the exact output, and the desired output.
– rmaddy
Nov 22 at 15:38
What’s the point of the date formatter? What’s the point of converting the Date to String to Date? And you need to provide specific details. Show the exact input, the exact output, and the desired output.
– rmaddy
Nov 22 at 15:38
I'm quite certain what you have is not an Unix timestamp
– mag_zbc
Nov 22 at 15:38
I'm quite certain what you have is not an Unix timestamp
– mag_zbc
Nov 22 at 15:38
Please add an example of
dateString
. And don't use NSString
in Swift.– vadian
Nov 22 at 15:39
Please add an example of
dateString
. And don't use NSString
in Swift.– vadian
Nov 22 at 15:39
This is a really poor question. As rmaddy says, you need to show an actual input date string and the desired output date string format, tell us the time zone you are converting to, and tell us how you conclude that the date is one hour off. (You say when you format the string to a date the hour is off, but why do you think that? A
Date
object doesn't have a time zone, and the default logging will display it in UTC.)– Duncan C
Nov 22 at 16:01
This is a really poor question. As rmaddy says, you need to show an actual input date string and the desired output date string format, tell us the time zone you are converting to, and tell us how you conclude that the date is one hour off. (You say when you format the string to a date the hour is off, but why do you think that? A
Date
object doesn't have a time zone, and the default logging will display it in UTC.)– Duncan C
Nov 22 at 16:01
I have now edited the question now
– kMose
Nov 22 at 16:42
I have now edited the question now
– kMose
Nov 22 at 16:42
|
show 5 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434193%2fhow-to-convert-unix-date-from-server-to-date-and-formatting-it-to-timezone-curr%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What’s the point of the date formatter? What’s the point of converting the Date to String to Date? And you need to provide specific details. Show the exact input, the exact output, and the desired output.
– rmaddy
Nov 22 at 15:38
I'm quite certain what you have is not an Unix timestamp
– mag_zbc
Nov 22 at 15:38
Please add an example of
dateString
. And don't useNSString
in Swift.– vadian
Nov 22 at 15:39
This is a really poor question. As rmaddy says, you need to show an actual input date string and the desired output date string format, tell us the time zone you are converting to, and tell us how you conclude that the date is one hour off. (You say when you format the string to a date the hour is off, but why do you think that? A
Date
object doesn't have a time zone, and the default logging will display it in UTC.)– Duncan C
Nov 22 at 16:01
I have now edited the question now
– kMose
Nov 22 at 16:42