I have a date in following format 2015-02-22T20:58:16+0000
In order to convert it to NSDate I found following solution
var df = NSDateFormatter()
df.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ"
var date = df.dateFromString(myDate)
df.dateFormat = "eee MMM dd yyyy"
var dateStr = df.stringFromDate(date!)
But I want to remove +0000 from date. I tried to remove ZZZZm but app crashes.
How can I remove extra +0000 digits ?
You ask:
How can I remove extra +0000 digits?
I'm not sure what you mean, because the resulting dateStr does have the +0000 removed.
But let's step back and consider the right way to parse a date string in the format of 2015-02-22T20:58:16+0000. You should use a locale of en_US_POSIX as described in Apple Technical Q&A 1480:
let myDate = "2015-02-22T20:58:16+0000"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
let date = dateFormatter.dateFromString(myDate)
When you then want to format that for the end user, reset the locale back to currentLocale:
dateFormatter.locale = NSLocale.currentLocale()
dateFormatter.dateFormat = "eee MMM dd yyyy"
let dateStr = dateFormatter.stringFromDate(date!)
The dateStr then becomes:
Sun Feb 22 2015
Or, perhaps better, for better localization behavior, use dateFormatFromTemplate:
let locale = NSLocale.currentLocale()
dateFormatter.locale = locale
dateFormatter.dateFormat = NSDateFormatter.dateFormatFromTemplate("eeeMMMdyyyy", options: 0, locale: locale)
let dateStr = dateFormatter.stringFromDate(date!)
In the US, it will appear like above, but in England it will appear as:
Sun, 22 Feb 2015
Or use one of the standard dateStyle values if that works for you. But I'd generally advise against using a hard-coded dateFormat, but rather use a format that honors the end-user's localization settings.
I wouldn't recommend removing time zone when you're parsing results. But if all you want to do is remove the string, you can do it like this:
let date = "2015-02-22T20:58:16+0000"
let display = date.substringToIndex(date.characters.indexOf("+")!)
This will give you the result of 2015-02-22T20:58:16
Related
I want to make the day come first instead of the month while using the dateStyle feature of DateFormatter(). I tried all possible ways I know but nothing seems to work.
Here is my code.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMM yyyy"
print(dateFormatter.string(from: beginDate)) //05 Jul 2020
dateFormatter.doesRelativeDateFormatting = true
dateFormatter.dateStyle = .medium
print(dateFormatter.string(from: beginDate)) //Jul 5, 2020
I want the last line to print "5 Jul, 2020". The dateStyle is important becuase without that the dateFormatter.doesRelativeDateFormatting will not work. There should be a simple way to do it. Please Help.
You don't actually need to change anything in your code. Just switch your Simulator's Region (Settings -> General -> Language & Region) to a place that uses the little endian (day, month, year) style (here's a list). A typical example is the UK.
If you want to always show the day first, regardless of the user's region, you can set the locale of the date formatter to one of those countries listed above:
dateFormatter.locale = Locale(identifier: "en-GB")
But I don't recommend this. You should not disrespect the user's language and region and their preferred date format unless you have a really good reason.
The string that is printed on the last command respects the locale of the user.
To do what you want, you can force it using
dateFormatter.locale = Locale(identifier: <identifier>)
To check a list of all the formats, you can do
Locale.availableIdentifiers.sorted().forEach { identifier in
dateFormatter.locale = Locale(identifier: identifier)
dateFormatter.doesRelativeDateFormatting = true
dateFormatter.dateStyle = .medium
print("identifier: \(identifier) - \(dateFormatter.string(from: beginDate))")
}
I am trying to save data to a database and am using a date that shows seconds as the key but I want to show a smaller date in the value that is going to be displayed. The problem is to use the date formatter you need to set it like this and I cant figure out how to set another one without overwriting the current format.
DateFormatter.dateFormat = "MM-dd-yy hh:mm:ss a"
This is how to have multiple DateFormatter in a single View.
Formatter1
let formatter = DateFormatter()
formatter.dateFormat = "QQQQ\ryyyy" //Shows Financial Quarter for Date
let formattedDate = formatter.string(from: date)
Formatter2
let formatter1 = DateFormatter()
formatter1.dateFormat = "EEEE, MMM d, yyyy" //Wednesday, Mar 29, 2017
let formattedDate1 = formatter1.string(from: date)
Refer to this site to know more about DateFormatter. Hope this helps. Happy Coding !
I have the following date format:
2016-08-26T05:16:50.200Z
And I am trying to parse similar date formatted strings into Date objects in Swift 3.0.
Here is my effort:
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-mm-dd EEEEE HH:mm:ss Z"
formatter.locale = Locale(identifier: "us")
var date = formatter.date(from: date_str)!
Where date_str is of the format mentioned above.
Unfortunately, swift don't recognize the format and returns nil.
Note: I don't want to change the string to match the formatter, but to adapt the formatter to the format of the string. String is of external source so I don't have the ability to change the string's format, but to stick with it and create a formatter that will recognize the string's date pattern.
Any ideas on where my format is wrong?
You are making mistake here, Here T is not for Week detail, The T is just a marker for where the time part begins.
A single point in time can be represented by concatenating a complete date expression, the letter T as a delimiter, and a valid time expression. For example "2016-10-05T14:30".
So now just change your dateFomat to yyyy-MM-dd'T'HH:mm:ss.SSSZ and you will get correct date you want.
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
formatter.locale = Locale(identifier: "us")
var date = formatter.date(from: date_str)!
print(date)
Output:
I am trying to convert a date from a String to NSDate with the following code:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "E, dd MMM yyyy, KK:mma"
date = dateFormatter.dateFromString(strDate)
I have also tried the following formats:
EEE, dd MMM yyyy, hh:mma
EEE, dd MMM yyyy, KK:mm
but the out put is always nil
Converted Tue, 12 Jan 2016, 7:30am to nil with the formatter E, dd MMM yyyy, KK:mma
I have also considered the follow StackOverflow posts:
String to NSDate in Swift
How can I convert string date to NSDate?
and I have been using this link as a reference for the date formats
The typical reason is that your device is not set to English language. Note that parsing strings like Tue or Jan is language specific. That means you have to make sure the formatter has a correct language set, e.g.:
dateFormatter.locale = NSLocale(localeIdentifier: "en")
Code :
let dateString = "2016-04-02"
var formatter: NSDateFormatter = NSDateFormatter()
formatter.timeZone = NSTimeZone(abbreviation: "GMT +3:00")
formatter.dateFormat = "yyyy-MM-dd"
println("dateString: \(dateString)")
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
let date = formatter.dateFromString(dateString)
println("date: \(date)")
formatter.dateFormat = "yyyy-MM-dd"
let formattedDateString = formatter.stringFromDate(date!)
println("formattedDateString: \(formattedDateString)")
Output :
dateString: 2016-04-02
date: Optional(2016-04-01 21:00:00 +0000)
formattedDateString: 2016-04-02
2016-04-01 21:00:00 +0000
I am trying to convert a string to NSDate datatype but not getting correct value. I have tried many solutions but its not returning correct value. I need it in yyyy-MM-dd format (2016-04-02) same as my input "2016-04-02". If someone can help would be really apriciated. Thanks in advance
I had the same problem and i this worked for me
You need to set the time zone
formatter.timeZone = NSTimeZone(abbreviation: "GMT+0:00")
When you convert from string to NSDate, if you do not set the timezone to the formatter, you will get the NSDate of a date in your local time zone. I suppose that your time zone is GMT+3 .
Then, when you show the value of 'date' (using println, NSLog but not NSDateFormatter), without setting the time zone, you will get GMT+0 time. That why you got 3h later.
Depend on how to use NSDateFormatter, you will have the date string as you want. In your case, It returns what you want, doesn't it?
Remember that NSDate presents a moment of time.
let dateString = "2016-04-02"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
println("dateString: \(dateString)")
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
let date = formatter.dateFromString(dateString) //without specify timezone, your dateString "2016-04-02" is your local time (GMT-3),
//means it's 2016-04-02 00:00:000 at GMT+0. That is the value that NSDate holds.
println("date: \(date)") //that why it show 2016-04-01 21:00:000, but not 2016-04-02 00:00:000
formatter.dateFormat = "yyyy-MM-dd"
let formattedDateString = formatter.stringFromDate(date!)
println("formattedDateString: \(formattedDateString)")
Your date is perfectly good. :-) No pun intended.
I will elaborate more on what #HoaParis has answered.
First of all NSDate represents a moment in time. It is not a date time value at the given place. So NSDate representing midnight in Greenwich will be 5:30 in morning in India.
Now coming to your question. When you give a date format with out time the formatter will assume it to be mid night. Also if there is not timezone mentioned it will take the current time zone.
So '2016-04-02' represents '2016-04-02, 00:00:00' at your time zone. Your timezone is GMT+3 that means when it is midnight at your place it is still 21:00:00 hours of previous day at Greenwich i.e. UK.
As we discussed NSDate is a moment in time to the same NSDate object represents these two seemingly different times but in reality they are the same time moment.
When you print the date by default it will print the date with respect to GMT and not your time zone i.e 2016-04-01, 21:00:00. The formatter will take into account your time zone and make it '2016-04-02'
let dateFormatter = DateFormatter()
//Your current Date Format
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
let finaldate = dateFormatter.date(from:"Your String")
//Your changing Date Format
dateFormatter.dateFormat = "MMM-dd"
let second = dateFormatter.string(from: finaldate!)