如何以 PST 格式获取当前日期?
How to get Current Date date in PST format?
我需要当前日期字符串。我已经尝试搜索 .我们目前从
得到的
[NSDate currentDate];
采用 UTC。要么告诉我如何在 PST 中转换它,要么直接在 PST 中获取当前日期。
NSDate *date = [NSDate date];
NSDateFormatter *formatterUTC = [[NSDateFormatter alloc] init];
formatterUTC.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[formatterUTC setDateFormat:@"MMM dd,yyyy, hh:mm a"];
NSString *dateString = [formatterUTC stringFromDate:date];
NSDate *dateFromString1 = [formatterUTC dateFromString:dateString];
NSDateFormatter *formatterPST = [[NSDateFormatter alloc] init];
[formatterPST setDateFormat:@"MMM dd,yyyy, hh:mm a"];
formatterPST.timeZone = [NSTimeZone timeZoneWithName:@"America/Los_Angeles"];
NSString *urdate = [formatterPST stringFromDate:dateFromString1];`
如果有人在 swift 中寻找解决方案:
public func getCurrentDateIn(timeZone: String) -> Date? {
// get a dateFormatter
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM dd,yyyy, hh:mm a"
let currentDateString : String = dateFormatter.string(from: Date())
// get currentDate in string format
guard let currentDate = dateFormatter.date(from: currentDateString) else { return nil }
// change the timezone of dateFormatter to timeZone
dateFormatter.timeZone = TimeZone.init(abbreviation: timeZone)
// get current date in timeZone in string format from dateFormatter
let timeZoneDateString = dateFormatter.string(from: currentDate)
// change the timeZone of dateFormatter to current otherwise it will apply conversion again.
dateFormatter.timeZone = TimeZone.current
return dateFormatter.date(from: timeZoneDateString)
}
//测试用例
getCurrentDateIn(timeZone: "IST")
"Apr 14, 2019 at 12:11 AM"
getCurrentDateIn(timeZone: "PST")
"Apr 13, 2019 at 11:41 AM"
getCurrentDateIn(timeZone: "SGT")
"Apr 14, 2019 at 2:41 AM"
我需要当前日期字符串。我已经尝试搜索 .我们目前从
得到的 [NSDate currentDate];
采用 UTC。要么告诉我如何在 PST 中转换它,要么直接在 PST 中获取当前日期。
NSDate *date = [NSDate date];
NSDateFormatter *formatterUTC = [[NSDateFormatter alloc] init];
formatterUTC.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[formatterUTC setDateFormat:@"MMM dd,yyyy, hh:mm a"];
NSString *dateString = [formatterUTC stringFromDate:date];
NSDate *dateFromString1 = [formatterUTC dateFromString:dateString];
NSDateFormatter *formatterPST = [[NSDateFormatter alloc] init];
[formatterPST setDateFormat:@"MMM dd,yyyy, hh:mm a"];
formatterPST.timeZone = [NSTimeZone timeZoneWithName:@"America/Los_Angeles"];
NSString *urdate = [formatterPST stringFromDate:dateFromString1];`
如果有人在 swift 中寻找解决方案:
public func getCurrentDateIn(timeZone: String) -> Date? {
// get a dateFormatter
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM dd,yyyy, hh:mm a"
let currentDateString : String = dateFormatter.string(from: Date())
// get currentDate in string format
guard let currentDate = dateFormatter.date(from: currentDateString) else { return nil }
// change the timezone of dateFormatter to timeZone
dateFormatter.timeZone = TimeZone.init(abbreviation: timeZone)
// get current date in timeZone in string format from dateFormatter
let timeZoneDateString = dateFormatter.string(from: currentDate)
// change the timeZone of dateFormatter to current otherwise it will apply conversion again.
dateFormatter.timeZone = TimeZone.current
return dateFormatter.date(from: timeZoneDateString)
}
//测试用例
getCurrentDateIn(timeZone: "IST") "Apr 14, 2019 at 12:11 AM"
getCurrentDateIn(timeZone: "PST") "Apr 13, 2019 at 11:41 AM"
getCurrentDateIn(timeZone: "SGT") "Apr 14, 2019 at 2:41 AM"