0

i am trying to use nsdateformatter and return a time set by NSTimeZone, but the output resets to GMT 0000, how can i return to the set timezone. it returns fine if i convert it from stringToDate. this only happens when i try and output it from datetoString

    let now = NSDate()
    let timezoneLK = NSTimeZone(forSecondsFromGMT: 19800)
    var formatter = NSDateFormatter()
    formatter.timeZone = timezoneLK
    formatter.dateStyle = NSDateFormatterStyle.LongStyle
    formatter.timeStyle = NSDateFormatterStyle.LongStyle

    let localDate = formatter.stringFromDate(now)
    let dateFrom = formatter.dateFromString(localDate)
    println("\(dateFrom)")

what am i missing, please help. i need to return a NSDate not a string.

for example the input time is (2015-06-29 15:34:28) this is the time at GMT +5:30, when i output the time with dateFromString it shows as (2015-06-29 10:03:31 +0000) is there a way to around it that it shows the NSDate as (2015-06-29 15:34:28).

  • NSDate represents an absolute point in time and does not have a time zone. The *description* of an NSDate always uses GMT. – Martin R Jun 29 '15 at 09:13
  • how can i set it to a time zone, the questions linked as duplicate "NSDate Format outputting wrong date" says to use NSDateFormatter, which i have above. is there a way around to set it to a specified timezone – Mugunthan Balakrishnan Jun 29 '15 at 09:20
  • 1
    You'll have to understand that NSDate **does not have a time zone**. If you want to print a date according to a time zone then you have to use a date formatter to convert it back to a string. – Martin R Jun 29 '15 at 09:21
  • let dateFormatter = NSDateFormatter() dateFormatter.timeZone = NSTimeZone(abbreviation: "GMT+5:30") dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle let dates = dateFormatter.stringFromDate(NSDate()) println ("\(dates)") – simbesi.com Jun 29 '15 at 09:21
  • check this will work let dates = dateFormatter.stringFromDate(NSDate()) here insted of stringFromDate you gave dateFromString – simbesi.com Jun 29 '15 at 09:23
  • i understand that NSDate does not have a timezone, i would like to know how can i go about setting it to a specific time instead of the output time. – Mugunthan Balakrishnan Jun 29 '15 at 09:27
  • as i mentioned raju, i need it in a NSDate format and not string – Mugunthan Balakrishnan Jun 29 '15 at 09:28
  • thats what @MartinR said first convert to string and pass where you pass date now – simbesi.com Jun 29 '15 at 09:30
  • Your code `dateFormatter.dateFromString(NSDate())` does not compile at all. dateFromString() must be called with a *string*, not with a *date*. Please show your actual code that *compiles*, and show the concrete input data, the output and explain which output you expect. – Martin R Jun 29 '15 at 09:31
  • Again: show the concrete input data, the output and **explain** which concrete output you expect. (I am still convinced that this is a duplicate.) – Martin R Jun 29 '15 at 09:46
  • i have updated the code @martin R. all i want is to output as NSDate() with the set time zone :) – Mugunthan Balakrishnan Jun 29 '15 at 09:51
  • 1
    @MugunthanBalakrishnan: Printing an NSDate **always uses the GMT time zone**. This is explained in the "duplicate", I have tried to explain it in above comments, and there are probably some dozen similar questions. – Martin R Jun 29 '15 at 09:54
  • i understand. is there a way around it @MartinR is what i want to know – Mugunthan Balakrishnan Jun 29 '15 at 09:57
  • @MugunthanBalakrishnan: Around what? You haven't even clearly described what **concrete (!) output** you expect for which **concrete (!) input**. – I am giving up on this. – Martin R Jun 29 '15 at 10:00
  • the input time is (2015-06-29 15:34:28) this is the time at GMT +5:30, when i output the time with dateFromString it shows as (2015-06-29 10:03:31 +0000) is there a way to around it that it shows the NSDate as (2015-06-29 15:34:28). – Mugunthan Balakrishnan Jun 29 '15 at 10:06
  • formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"; use this extra line of your code. this will give what your are expecting – simbesi.com Jun 29 '15 at 10:53
  • for your requirement these 3 lines is enough: var formatter: NSDateFormatter = NSDateFormatter() formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" let dateTime: String = formatter.stringFromDate(NSDate()) println("\(dateTime)") – simbesi.com Jun 29 '15 at 10:57

0 Answers0