I have string - Dec 13, 10:00 AM IST
How to convert this string to date anyone suggest ?
I created this date formatter but getting null -
NSDateFormatter *userTimeFormatter = [[NSDateFormatter alloc] init];
[userTimeFormatter setDateStyle:NSDateFormatterMediumStyle];
[userTimeFormatter setTimeStyle:NSDateFormatterShortStyle];
userTimeFormatter.doesRelativeDateFormatting = YES;
[userTimeFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDateFormatter *userTimeFormatter = [[NSDateFormatter alloc] init];
NSString *dateString = #"Dec 13, 10:00 AM IST";
NSTimeZone *timeZone = [NSTimeZone timeZoneWithAbbreviation:#"IST"];
[userTimeFormatter setTimeZone:timeZone];
dateString = [dateString stringByReplacingOccurrencesOfString:#" IST" withString:#""];
[userTimeFormatter setDateFormat:#"MMM d, hh:mm a"];
NSDate *date = [userTimeFormatter dateFromString:dateString];
NSLog(#"india date : %#", date);
This will give you the IST TimeZone NSDate, from this date you can convert to system timezone with:
[userTimeFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *myDate = [userTimeFormatter dateFromString:dateString];
NSLog(#"my date : %#", myDate);
Output:
india date : 2000-12-13 04:30:00 +0000
my date : 2000-12-13 06:00:00 +0000
Related
My date and time is 20-Nov-2019 21:09 Which is in UTC 24 hours format. now I want to convert it into local time in 12 hours formate. 30-Nov-2019 08:00 AM like this.
My code is :
// create dateFormatter with UTC time format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
// change to a readable time format and change to local time zone
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:date];
My code when i send my local time 12 formate into 24 hours UTC
-(NSString *)getUTCFormateDate:(NSDate *)localDate
{
// NSLog(#"%#", localDate);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm"];
NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
dateFormatter.locale = twelveHourLocale;
NSTimeInterval timeZoneoffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
NSTimeInterval utcTimeInterval = [localDate timeIntervalSinceReferenceDate] - timeZoneoffset;
NSDate *utcCurrentDate = [NSDate dateWithTimeIntervalSinceReferenceDate:utcTimeInterval];
NSString *dateString = [dateFormatter stringFromDate:utcCurrentDate];
// NSLog(#"dateString %#", dateString);
return dateString;
}
-(NSDate *)getUTCDate:(NSString *)currentDate{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MMM-yyyy HH:mm"];
NSDate *date1 = [dateFormat dateFromString:currentDate];
if (date1 == nil){
[dateFormat setDateFormat:#"dd-MMM-yyyy hh:mm a"];
date1 = [dateFormat dateFromString:currentDate];
}
return date1;
}
I think you are doing too much. The format "hh" is the hour in 12-hour format, HH is 24-hour format. You should not have to set the locale for that (though setting to en_US_POSIX does avoid the user's 24-hour preference in the [NSLocale currentLocale] instance which can override that on iOS).
NSDate is an absolute instance in time. You need to apply a calendar and time zone with an NSDateFormatter to get numeric year/month/day etc. values out of it, but you don't need to adjust the offset to the reference date (that is changing the actual date, not just reformatting it in a different time zone).
NSString *utcString = #"20-Nov-2019 21:09";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
formatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
formatter.dateFormat = #"dd-MMM-yyyy HH:mm";
NSDate *date = [formatter dateFromString:utcString];
formatter.timeZone = [NSTimeZone localTimeZone]; // GMT-5 for me
formatter.dateFormat = #"dd-MMM-yyyy hh:mm a";
NSLog(#"date: %#", [formatter stringFromDate:date]);
// date: 20-Nov-2019 04:09 PM
I want to change my date format from "MM/dd/YYYY HH:mm:ss" to "EEE dd MMM yyyy hh:mm a", but the code I am using not working for example if my input is 11/30/2016T01:04:30 I am getting the month changed as December, can any one help where is the mistake?
NSString * date = #"11/30/2016T01:04:30";
date = [date stringByReplacingOccurrencesOfString:#"T" withString:#" "];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM/dd/YYYY HH:mm:ss"];
NSDate *myDate = [df dateFromString: date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE dd MMM yyyy hh:mm a"];
NSString *dayName= [dateFormatter stringFromDate:myDate];
NSLog(#"the converted Day %#",dayName);
no need of this
// date = [date stringByReplacingOccurrencesOfString:#"T" withString:#" "];
use like
NSDateFormatter *df = [[NSDateFormatter alloc] init];
NSString * date = #"11/30/2016T01:04:30";
[df setDateFormat:#"MM/dd/yyyy'T'HH:mm:ss"];
NSDate *myDate = [df dateFromString: date];
[df setDateFormat:#"EEE dd MMM yyyy hh:mm a"];
NSString *dayName= [df stringFromDate:myDate];
NSLog(#"the converted Day %#",dayName);
Output:
the converted Day Wed 30 Nov 2016 01:04 AM
I am trying to reformat this NSdate (2016-02-11 01:00:00 +0000 ) into this (11-02-2016).
I have used below code and its returning null.
//newDate format is 2016-02-11 01:00:00 +0000
NSString *dateString = [[self dateFormatter] stringFromDate:newDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateString];
// Convert date object into desired format
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *newDateString = [dateFormatter stringFromDate:date];
NSLog(#"people%#",newDateString); //printing null
Easiest way
NSString *strDateFor = #"2016-02-11 01:00:00 +0000";
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
[dateformate setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZZ"];
NSDate *date = [dateformate dateFromString:strDateFor];
NSLog(#"date : %#",date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"dd-MM-yyyy"];
NSString *strDate = [dateFormat stringFromDate:date];
NSLog(#"The strDate is - %#", strDate);
The Console output is
date : 2016-02-11 01:00:00 +0000
The strDate is - 11-02-2016
NSString *myString = #"2016-02-11 11:29:04";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSDate *yourDate = [dateFormatter dateFromString:myString];
dateFormatter.dateFormat = #"dd-MMM-yyyy";
NSLog(#"%#",[dateFormatter stringFromDate:yourDate]);
+0000 it the time zone definition. You should parse it with Z
try this
[dateformat setDateFormat:#"yyyy-MM-dd HH:mm:ssZZZZ"];
else if your date is current date use
[dateformat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Hi I am parsing an end point of API, from there I am getting a string which contain like this date string 2016-01-18T13:28:06.357 but I want to convert this like 01 Jan 16, I have tried many times by using different format one of following mentioned. kindly need suggestion how to accomplish this task.
NSString * date = [dictionary objectForKey:#"date"];
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[dateformat setDateFormat:#"dd MMM yyyy"];
NSDate *myDate = [dateformat date];
// Objective C
NSString * date = [dictionary objectForKey:#"date"];
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[dateformat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *myDate = [dateformat dateFromString:date];
[dateformat setDateFormat:#"dd MMM yyyy"];
NSString *strDate=[dateformat stringFromDate:myDate];
// Swift 3.0
var date = (dictionary["date"] as! String)
var dateformat = DateFormatter()
dateformat.timeZone = NSTimeZone(name: "EST")
dateformat.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
var myDate = dateformat.date(fromString: date)!
dateformat.dateFormat = "dd MMM yyyy"
var strDate = dateformat.string(from: myDate)
You should use this format:
NSString *dateStr = #"2016-01-18T13:28:06.357";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *date = [formatter dateFromString:dateStr];
// date is 2016-01-18 08:28:06 +0000
And next convert that date to format you need
You can also use this :
NSString *dateString = #"2016-01-18T13:28:06.357";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [dateFormatter dateFromString:dateString];
// Convert date object into desired format
[dateFormatter setDateFormat:#"dd MMMM yy"];
NSString *newDateString = [dateFormatter stringFromDate:date];
NSLog(#"newDateString=%#",newDateString);
You should use this format code
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//Get Date
[formatter setDateFormat:#"dd"];
NSString *strDate = [formatter stringFromDate:[NSDate date]];
NSLog(#"Date is - %#",strDate);
//Get Month
[formatter setDateFormat:#"MMM"];
NSString *strMonth = [formatter stringFromDate:[NSDate date]];
NSLog(#"Month is - %#",strMonth);
//Get Year
[formatter setDateFormat:#"yyyy"];
NSString *strYear = [formatter stringFromDate:[NSDate date]];
NSLog(#"Year is - %#",strYear);
Log is :
Date is - 05
Month is - Feb
Year is - 2016
for set more format, Click Me
NSDate *date = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"hh:mm a"];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setLocale:[NSLocale systemLocale]];
NSLog(#" >> %# >>> %#",date,[df dateFromString:#"12:18 AM"])
And log is
>> 2014-04-10 07:00:48 +0000 >>> 2000-01-01 06:52:00 +0000
I want that the second date has also the same date as the first.
like
2014-04-10 07:00:48 +0000 >>> 2014-04-10 06:52:00 +0000
If you want to convert the time string "12:18 AM" to a date for the current day then you can proceed as follows:
NSString *time = #"12:18 AM";
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
// Get year-month-day for today:
[fmt setDateFormat:#"yyyy-MM-dd "];
NSString *todayString = [fmt stringFromDate:[NSDate date]];
// Append the given time:
NSString *todaysTime = [todayString stringByAppendingString:time];
// Convert date+time string back to NSDate:
[fmt setDateFormat:#"yyyy-MM-dd h:mm a"];
NSDate *date = [fmt dateFromString:todaysTime];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy hh:mm:ss a"];
By using this formatter, you can get NSDate