how do i convert a string to specific format - ios

I have referred lot of links but could not find the solution hence asking it again.I have string which i need to convert into date format. Below is example
Input string : "1410271500"
In above string 14 is for 2014
10 is for Oct
27 is day
1500 is 3:00 PM
Output string : "2014-10-27 03:00:00"
Input string in not in long-integer format.
Thanks

do like
NSString *getMilliSec = #"1410271500";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyMMddHHmm"];
NSDate *getDate = [formatter dateFromString:getMilliSec];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss a"]; // or use yyyy-MM-dd hh:mm:ss
formatter.timeZone = [NSTimeZone systemTimeZone]; // optional
NSString *stringFromDate = [formatter stringFromDate:getDate];
swift3
let getMilliSec = "1410271500"
let formatter = DateFormatter()
formatter.dateFormat = "yyMMddHHmm"
let getDate = formatter.date(from: getMilliSec)
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss a" // or use yyyy-MM-dd hh:mm:ss
formatter.timeZone = NSTimeZone.system // optional
var stringFromDate = formatter.string(from: getDate!)
output

Related

Convert date string into proper format

I am getting a response from a server and there is a date string I need to convert into a date:
Thu Jun 29 07:15:25 +0000 2017
I am trying to convert the string into a human readable format. Can anyone suggest how to convert this string into date?
You need to parse the date string into a Date object using a DateFormatter, then create a string from that Date using another DateFormatter with the output format you want to use.
let created_at = "Thu Jun 29 07:15:25 +0000 2017"
let df = DateFormatter()
df.dateFormat = "E MMM dd HH:mm:ss Z yyyy"
guard let date = df.date(from: created_at) else {return}
let outputFormatter = DateFormatter()
outputFormatter.dateFormat = "EEE, MMM d"
let outputDate = outputFormatter.string(from: date)
If you want to know what date formats look like, use NSDateFormatter.com, which has an interactive interface where you can play around with different date formats and have examples of most common formats as well.
This is how you can format it in "Thu Jun 29"
-(NSString *)formatCreatedAt:(NSString *)createdAt
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE MMM dd HH:mm:ss Z yyyy"];
NSDate *date = [dateFormat dateFromString:createdAt];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE MMM dd"];
NSString *stringFromDate = [formatter stringFromDate:date];
return stringFromDate;
}
Pass the value of created_at in this method and the output will be returned in your specified format.

How to get Current Date date in PST format?

I need current date string . I have already tried searching . What we get currently from
[NSDate currentDate];
is in UTC. Either tell me how to convert it in PST or directly get current date in 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];`
In case someone is looking for a solution in 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)
}
//Test Cases
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"

how to show time 08:00:00 to 8 am using swift

I'm receiving time in string as 08:00:00 and I need to show it as 8 am.
I have no idea how to do it so guys help me.
I don't know how to do it using NSDate.
do like
Objective-C
NSString *sample = #"08:00:00";
NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm:ss"]; // or use hh:mm:ss
NSDate *date1 = [dateFormatter dateFromString:sample];
[dateFormatter setDateFormat:#"HH a"]; // or use hh
NSString *finaldate = [dateFormatter stringFromDate:date1];
NSLog(#"%#",finaldate);
Swift
var sample: String = "08:00:00"
var dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm:ss" // or use hh:mm:ss
var date1: NSDate = dateFormatter.dateFromString(sample)
dateFormatter.dateFormat = "HH a" // or use hh
var finaldate: String = dateFormatter.stringFromDate(date1)
NSLog("%#", finaldate)
Time Format
// 24 hours fomat
HH -- 13, 14, etc
// 12 hours format
hh -- 01, 02, etc

Convert date-time in 12hrs format to 24hrs format in iOS

2015-05-29 06:10 PM convert this date into system timezone 24hrs formate
convert this date into 2015-05-29 18:10:00
You can use this code to convert 12 hour format date into 24 hours date.
NSDate *date = your date in 12 hours format;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:#"dd-MM-YYYY HH:mm"];
NSString *dateStringInTwentyHours = [dateFormatter stringFromDate:date];
For Swift 3.0
You can use this code snippet to convert 12 hour to 24 hour format.
let sourceDateString = "2015-05-29 10:40 AM"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd hh:mm a"
formatter.locale = Locale(identifier: "en-US")
let date = formatter.date(from: sourceDateString)
print("\(date)")
formatter.timeZone = NSTimeZone.local
formatter.dateFormat = "yyyy-MM-dd HH:mm"
let strDate = formatter.string(from: date!)
print(strDate)

How do I convert the string(2014-07-17T09:00:00-04:00) to the format below? yyyy-MM-dd HH:mm:ss

I have a string to convert to date.
The response string is
2014-07-17T09:00:00-04:00
where:
yyyy-MM-ddTHH:mm:ss-(04:00 is the offset)
How do I convert the "2014-07-17T09:00:00-04:00" as string to the format below?
yyyy-MM-dd HH:mm:ss
if that is an Objective-C question, I would do this:
NSString *_stringDate = #"2014-07-17T09:00:00-04:00";
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *_date = [_dateFormatter dateFromString:_stringDate];
[_dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *_stringWithRequestedDateFormat = [_dateFormatter stringFromDate:_date];
NSLog(#"%#", _stringWithRequestedDateFormat);
if that is a Swift question, I would do that:
let stringDate: String = "2014-07-17T09:00:00-04:00"
var dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date: NSDate = dateFormatter.dateFromString(stringDate)
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let stringWithRequestedDateFormat: String = dateFormatter.stringFromDate(date)
println(stringWithRequestedDateFormat)
the console shows in both cases:
2014-07-17 14:00:00

Resources