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!










share|improve this question




















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










  • I have now edited the question now
    – kMose
    Nov 22 at 16:42















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!










share|improve this question




















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










  • I have now edited the question now
    – kMose
    Nov 22 at 16:42













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!










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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










  • I have now edited the question now
    – kMose
    Nov 22 at 16:42














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










  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Trompette piccolo

Slow SSRS Report in dynamic grouping and multiple parameters

Simon Yates (cyclisme)