I have variables that I want to use to create a string that can be printed to a UILabel.
maturityDate | ComparisonTerm | monthsToMaturity | ComparisonRate
The following are variables:
maturitDate:Date // value (value to return MMM DD, YYYY) Ex June 23, 2017
ComparisonTerm: Double
MonthsToMaturity: Double
ComparisonRate: Double
I want the "|" bars to be separators. My biggest challenge has been dealing with the date value. It currently returns 2021-09-01 04:00:00 + 000 as an example.
It should be pretty simple for the other three variable like this
"\(ComparisonTerm) | \(monthsToMaturity) | \(ComparisonRate)"
For the date, you have to use a date formatter
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .mediumStyle // or .shortStyle
dateFormatter.timeStyle = .noStyle
So the required date String is basically
"\(dateFormatter.dateFromString(maturitDate)) | \(ComparisonTerm) | \(monthsToMaturity) | \(ComparisonRate) "
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "MMM dd,yyyy"
let maturityDate = Date()
let maturityDateStr = dateFormatterPrint.string(from: maturityDate)
let comparisonTerm : Double = 0.1
let monthsToMaturity : Double = 0.2
let comparisonRate : Double = 0.3
let str = "\(maturityDateStr) | \(comparisonTerm) | \(monthsToMaturity) | \(comparisonRate)"
Related
This question already has an answer here:
NSDateFormatter show wrong year
(1 answer)
Closed 3 years ago.
I try to format my UTC date but the result seems very wrong
DateFormatter:
let df = DateFormatter()
df.calendar = Calendar(identifier: .iso8601)
df.dateFormat = "MMMM dd YYYY hh mm"
df.locale = Locale(identifier: "en_US_POSIX")
df.timeZone = TimeZone(identifier: "UTC")
Example 1
Date 1:
po dateStart
▿ 2018-12-31 00:00:00 +0000
- timeIntervalSinceReferenceDate : 567907200.0
Giving:
po df.string(from: dateStart)
"December 31 2019 12 00"
Example 2
Date 2:
po dateEnd
▿ 2021-01-03 23:59:59 +0000
- timeIntervalSinceReferenceDate : 631411199.0
Giving:
po df.string(from: dateEnd)
"January 03 2020 11 59"
Why does this happen and how can I get the correct Date (Year)?
You should use "MMMM dd yyyy hh mm" instead of "MMMM dd YYYY hh mm" rest of seems ok to me.
I am getting timeStamp from API as EST, if my device is in India or in USA then it should show (IST or CST) the time according to the timezone. How can I do it?
let currentTimeZone = formatter.timeZone.identifier
print(currentTimeZone) // Asia/Kolkata
How can I get EST, CST, EDT, etc. instead of getting Asia/Kolkata?
You can get the name from the name property:
NSLog(#"Name : %#", timeZone.name);
Output will be :
2015-07-10 13:31:46.292 APP[2904:50429] Name : Asia/Calcutta
Then You can to get the name with the desired style:
NSTimeZone* timeZone = [NSTimeZone timeZoneWithAbbreviation:#"IST"];
NSString* timeZoneName = [timeZone localizedName:NSTimeZoneNameStyleStandard locale:[NSLocale currentLocale]];
NSLog(#"Name : %#", timeZoneName);
NSLog(#"%#",[NSTimeZone abbreviationDictionary]);
output will be:
2015-07-10 11:44:05.954 APP[1472:26120] {
ADT = "America/Halifax";
AKDT = "America/Juneau";
AKST = "America/Juneau";
ART = "America/Argentina/Buenos_Aires";
AST = "America/Halifax";
BDT = "Asia/Dhaka";
BRST = "America/Sao_Paulo";
BRT = "America/Sao_Paulo";
BST = "Europe/London";
CAT = "Africa/Harare";
CDT = "America/Chicago";
CEST = "Europe/Paris";
CET = "Europe/Paris";
CLST = "America/Santiago";
CLT = "America/Santiago";
COT = "America/Bogota";
CST = "America/Chicago";
EAT = "Africa/Addis_Ababa";
EDT = "America/New_York";
EEST = "Europe/Istanbul";
EET = "Europe/Istanbul";
EST = "America/New_York";
GMT = GMT;
GST = "Asia/Dubai";
HKT = "Asia/Hong_Kong";
HST = "Pacific/Honolulu";
ICT = "Asia/Bangkok";
IRST = "Asia/Tehran";
IST = "Asia/Calcutta";
JST = "Asia/Tokyo";
KST = "Asia/Seoul";
MDT = "America/Denver";
MSD = "Europe/Moscow";
MSK = "Europe/Moscow";
MST = "America/Denver";
NZDT = "Pacific/Auckland";
NZST = "Pacific/Auckland";
PDT = "America/Los_Angeles";
PET = "America/Lima";
PHT = "Asia/Manila";
PKT = "Asia/Karachi";
PST = "America/Los_Angeles";
SGT = "Asia/Singapore";
UTC = UTC;
WAT = "Africa/Lagos";
WEST = "Europe/Lisbon";
WET = "Europe/Lisbon";
WIT = "Asia/Jakarta";
}
then you can find what is your current timezone.
let timeStr = "15/02/2014 01:00:00"
let currentTimezone = TimeZone.current
let targetTimezone = TimeZone(abbreviation: "EST") ?? TimeZone.current
let dateFormatter1 = DateFormatter()
dateFormatter1.dateFormat = "dd/MM/yyyy HH:mm:ss"
let localOffset = currentTimezone.secondsFromGMT()
let targetOffset = targetTimezone.secondsFromGMT()
let localDate = dateFormatter1.date(from: timeStr)
let targetDate = localDate?.addingTimeInterval(TimeInterval(targetOffset - localOffset))
Try this. TimeZone.current will return you the current timezone used by system.
try this code:
let timeZone = TimeZone.current
let name = timeZone.localizedName(for: .shortStandard, locale: Locale.current)
print("Name :: ", name)
Please try this.It works for me.
Please get the time from server from UTC format.
then :
func convertFromUTCtoLocal(dateToConvert:String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssz"
let convertedDate = formatter.date(from: dateToConvert)
formatter.timeZone = TimeZone(abbreviation: "EST")
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return formatter.string(from: convertedDate!)
}
Set the timezone according to your requirement.It may helps to you. Thank you
ObjC:
NSTimeZone *timeZoneLocal = [NSTimeZone localTimeZone];
NSString *strTimeZoneAbb = [timeZoneLocal abbreviation];
NSString *strTimeZoneName = [timeZoneLocal name];
NSLog(#"%#",strTimeZoneAbb); //Ex:Output:GMT +5:10
NSLog(#"%#",strTimeZoneName); //Ex:Output:Asia/Calcutta
Swift:
let timezone = NSTimeZone.local
print(timezone.abbreviation)
print(timezone.name)
I'm using NSDateFormater to format dates that i get from my server.
All the formatting works correctly except of AM/PM
let f = "EEEE, MMM dd, hh:mma"
dateFormatter.dateFormat = f
let d = dateFormatter.stringFromDate(NSDate())
print("formated date: \(d)")
i get:
formated date: Tuesday, Feb 23, 11:30
and it should be:
formated date: Tuesday, Feb 23, 11:30am
Am i missing something?
Solution:
My IPhones time is set to 24 hour format, i changed it to 12 and it also changed in my app.
I think it should be
let f = "EEEE, MMM dd, hh:mm a"
I just tested this and it works
let dateFormatter = NSDateFormatter()
let f = "EEEE, MMM dd, hh:mm a"
dateFormatter.dateFormat = f
let d = dateFormatter.stringFromDate(NSDate())
print("formated date: \(d)")
Demo here
I got exception when using the following code :
var expDate : NSDate = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
expDate = dateFormatter.dateFromString("Sun Apr 19 10:33:18 GST 2009")
I've tried e and E instead of EEE , Z and ZZZ instead of z
but didn't work
does anyone know what's the problem?
It appears that "GST" isn't a supported abbreviation. See https://gist.github.com/norio-nomura/d4d2475d62e446f796d8 for want of an official source.
I'm using CPTAxisLabelingPolicyAutomatic on my x-axis which for time-line for days of a year (e.g. 07 March 2015).
This is my reference date:
let components = NSDateComponents()
components.month = 10
components.day = 29
components.year = 1970
components.hour = 0
components.minute = 0
components.second = 0
referenceDate = NSCalendar.currentCalendar().dateFromComponents(components)!
and here is part of my x axis style
x.labelingPolicy = .Automatic
let oneDay:NSTimeInterval = 60 * 60 * 24
x.majorIntervalLength = oneDay
x.minorTicksPerInterval = 0
let formatter = NSDateFormatter()
formatter.dateStyle = .MediumStyle
formatter.timeStyle = .NoStyle
let timeFormatter = CPTTimeFormatter(dateFormatter: formatter)
timeFormatter.referenceDate = referenceDate
x.labelFormatter = timeFormatter
The problem is eventhough, the x values are NSDate at 00:00:00 of the days they do not stick to the gridline for that specific date:
but if I use .FixedInterval policy I get the correct behaviour but I loose the autmatic labeling on pinching.
How can I benefit from the two world?
The automatic labeling algorithm doesn't know about dates, so it won't split the axis into "nice" date intervals like days or weeks. Right now, the fixed interval labeling policy is the best solution. It is up to you to determine the proper interval based on the range being displayed. There is an open issue to add proper date support, but it's pretty low priority and I can't guarantee when it will be done.