-1

So I have a huge file of Unix Timestamps and I would like to convert these into a date.

This is how my .txt file looks.

1529837880,xx.xx,xx.
1529837940,xx.xx,xx.
1529838000,xx.xx,xx.
1529838060,xx.xx,xx.
1529838120,xx.xx,xx.

I already created a string that stores all these numbers.

the format I want should look like this:

20130616 060300;xx.xx,xx. // 2013 06 16 - 06:03
20130616 060400;xx.xx,xx.
20130616 060500;xx.xx,xx.
20130616 060600;xx.xx,xx.
20130616 060700;xx.xx,xx.
20130616 060800;xx.xx,xx.

my string looks like this

var str = """

    1529837880,xx.xx,xx.
    1529837940,xx.xx,xx.
    1529838000,xx.xx,xx.
    1529838060,xx.xx,xx.
    1529838120,xx.xx,xx.

"""

So how would I do this?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
iOS Koray
  • 41
  • 1
  • 9
  • When reviewing the answers in the duplicate be sure you look at the ones using `Date` and not the older ones using `NSDate`. – rmaddy Jun 24 '18 at 15:43

1 Answers1

0

You can use Date.init(timeIntervalSince1970:) to convert a Unix timestamp (represented as a TimeInterval aka Double) into a Date value.

The second step would then be to create and configure a suitable DateFormatter to output the date as a string in your desired format (using the DateFormatter.string(from:) method).

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • so you mean `str.Date.init(NSTimeIntervalSince1970)` ? because im getting a error: `value of type 'String' has no member 'Date'` – iOS Koray Jun 24 '18 at 15:10