time is string format like 10:27:43. but when i convert in to date format. it is going to change Sat Jan 1 10:27:43 2000.but i need only time like 10:27:43
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss ZZZ yyyy"];
NSLog(#"Date is %#", dateTime);
// Convert to new Date Format
[dateFormatter setDateFormat:#"HH:mm:ss"];
newDate = [dateFormatter stringFromDate:dateTime];
NSLog(#" string Date is %#", newDate);
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init] ;
[dateFormatter1 setDateFormat:#"HH:mm:ss"];
//[dateFormatter1 setDateFormat:#"dd/mm/yyyy"];
NSDate *dateFromString = [dateFormatter1 dateFromString:newDate];
NSLog(#"date format date %#",dateFromString);
you need to convert dateFormat as per date is coming and after convert it as you want.
Try this below code..
NSString *dateTime = #"Thu Jan 25 10:38:11 2018";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss yyyy"];
NSDate *dateFromString = [dateFormatter dateFromString:dateTime];
NSLog(#"Date is %#", dateTime);
// Convert to new Date Format
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSString *result = [dateFormatter stringFromDate:dateFromString];
NSLog(#"result %#",result);
Split the time in date components and use that components as a time.
Check below code.
NSString *aStrTime = #"10:27:43";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:aStrTime];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSInteger sec = [components second];
NSLog(#"%d:%d:%d", hour, minute, sec);
Note: change aStrTime and dateFormatPattern according to your need.
You can try the following code:-
For e.g:- if you are getting the date in 10-02-2017 12:30:50 format
NSString *strDate = #"10-02-2017 12:30:50";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-mm-yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:strDate];
NSLog(#"The Date is: %#", date);
// By this code you will get the date in NSDate form. Now you can change the format to get the time
[dateFormat setDateFormat:#"HH:mm:ss"];
NSString *strFinalTime = [dateFormatter stringFromDate:strDate];
NSLog(#"strFinalTime: %#",strFinalTime);
Related
I currently get time as UTC and I want it to be convert into local time zone
NSDate * startDate;
Example strDate - 14/9/2017 7.28 Am
Required format - 14/9/2017 1.52 pm
I need help with objective c.
NSDate * startDate ; //contain utc date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, MMM d, yyyy - h:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:startDate];
but this is not correct time but date is correct
First convert NSString to NSDate.
NSDateFormatter *DateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZoneEDT =[NSTimeZone timeZoneWithName:#"GMT"];
[DateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[DateFormatter setTimeZone:timeZoneEDT];
[DateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS"];
NSDate *CurrentDate = [DateFormatter dateFromString:DateStr];
Then convert GMT time to local time.
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:#"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
NSString *dateRepresentation = [dateFormatter stringFromDate:dateFromString];
#athira try this. it works for me.
NSDate *startDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/M/yyyy h.mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:startDate];
NSLog(#"date = %#",timestamp); //date = 14/9/2017 4.34 PM
it will print current time with proper GMT+5:30 and i have used localTimeZone.
I'm getting time in HH:mm:ss format from web service and it is in Argentina(GMT-3) time zone. I want to convert this time into full date (dd-MM-yyyy HH:mm:ss) and finally convert it into device local time zone's date.
Here is my code
-(NSString *)getLocalTimeStringFrom:(NSString *)sourceTime
{
static NSDateFormatter* df = nil;
static NSDateFormatter* df1 = nil;
if (!df) {
df = [[NSDateFormatter alloc]init];
df.dateFormat = #"dd-MM-yyyy HH:mm:ss";
}
if (!df1) {
df1 = [[NSDateFormatter alloc]init];
df1.dateFormat = #"dd-MM-yyyy";
}
NSTimeZone *sourceZone = [NSTimeZone timeZoneWithAbbreviation:#"ART"];
[df setTimeZone: sourceZone];
[df1 setTimeZone: sourceZone];
NSString *artDate = [df1 stringFromDate:[NSDate date]]; // get 29-09-2015
NSString* timeStamp = [artDate stringByAppendingString:[NSString stringWithFormat:#" %#",sourceTime]]; // get 29-09-2015 00:05:00, if sourceTime is 00:05:00
NSDate *art = [df dateFromString:timeStamp];
NSLog(#"ART date %#" , art);//ART date 2015-09-29 03:05:00 +0000
NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
[df setTimeZone: localTimeZone];
NSLog(#"Local date %#" , [df stringFromDate: art]);
return [df stringFromDate: ds];
}
The problem is that i'm getting UTC date in art (as commented in code),please note that the value of art is 2015-09-29 03:05:00 +0000 which is in UTC and format is yyyy-MM-dd HH:mm:ss, but is should be in ART and dd-MM-yyyy HH:mm:ss. I tried some code from net but didn't get solution. What is wrong in this code?
I use this:
NSDateFormatter* serverDf = [[NSDateFormatter alloc] init];
[serverDf setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:-3*60*60]];
[serverDf setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSDate* gmtDate = [serverDf dateFromString:timeStamp];
// DDLogVerbose(#"DateTime in GMT: %#", gmtDate);
NSDateFormatter* localDF = [[NSDateFormatter alloc] init];
[localDF setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
[localDF setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
NSString *localDate = [localDF stringFromDate:gmtDate];
May you just want a formate NSString , but not a NSDate,
Because I think NSDate will add a '+0000'
Try this.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:yourString];
NSString *str = [dateFormatter stringFromDate:date];
I am converting this date string "05/20/2015 05:27 pm" into NSDate with the help of formatter but it returns nil.
-(NSDate *)dateFromString:(NSString *)strDate withFormat:(NSString *)strDateFormat
{
NSDate *date = nil;
[self.dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
if (strDate) {
if (strDateFormat) {
[self.dateFormatter setDateFormat:strDateFormat];
}
date = [self.dateFormatter dateFromString:strDate];
}
return date;
}
I am using the above function where I am passing the string as "05/20/2015 05:27 pm" with format #"dd/MM/yyyy HH:mm:ss".
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"mm/dd/yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *stringDate = [dateFormatter dateFromString:#"05/20/2015 05:27 pm"];
NSLog(#"%#", stringDate);
-(NSDate *)dateFromString:(NSString *)strDate withFormat:(NSString *)strDateFormat
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:strDateFormat];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date = [dateFormatter dateFromString:strDate];
return date;
}
NSString *dateStr = #"05/20/2015 05:27 pm";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString:dateStr];
Nslog(#"%#",date);
To transform from NSDate to timestamp you can use:
NSDate *date = [NSDate date];
NSTimeInterval timestampSeconds = [date timeIntervalSince1970];
Result will be in seconds. To transform timestamp to standard milliseconds just multiply by 1000.
timeIntervalSince1970
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"mm/dd/yyyy hh:mm a"];
NSDate *strDate = [formatter dateFromString:#"05/20/2015 05:27 pm"];
NSLog(#"%#", strDate);//NSDate
time_t timeStamp = (time_t) [strDate timeIntervalSince1970];
NSLog(#"%ld", timeStamp);//timestamp
How to convert NSDate into unix timestamp iphone sdk?
This might helps you :)
I am trying to get date in the format Monday, March 09, 2015. But following code is not returning required date format. I think I am using wrong Formatter. Here is the code:
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMMM yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
try this...
//Getting date from string
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
// converting into our required date format
[dateFormatter setDateFormat:#"EEEE, MMMM dd, yyyy"];
NSString *reqDateString = [dateFormatter stringFromDate:date];
NSLog(#"date is %#", reqDateString);
LOG:2015-03-09 12:40:33.456 TestCode [1377:38775] date is Monday, March 09, 2015
Firstly you must convert your dateString to NSDatevalue
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
after that you could use this format: #"EEEE,MMMM dd, yyyy" to convert that dateValue to dateString like Monday, March 09, 2015
Hope this help
Helpful link for you
Try to set dateFormat property to #"dd'-'MM'-'yyyy" . Always check the unicode the date symbol table. "MMMM" means that the date month should be a full month name.
Try this one..
NSString * yourJSONString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];;
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *dateFromString = [dateFormatter dateFromString:yourJSONString];
[dateFormatter setDateFormat:#"EEEE,LLLL dd, yyyy"];
NSString *output = [dateFormatter stringFromDate:dateFromString];
NSLog(#"%#", output);
Try this:-
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString * format = [NSDateFormatter dateFormatFromTemplate:#"EEEEMMMMdyyyy" options:0 locale:[NSLocale currentLocale]];
[formatter setDateFormat:format];
NSString *dateFormatted = [fullDateFormatterTime stringFromDate: date];
NSLog(#"Formatted date: %#", dateFormatted);
I have my data in JSON format which contains some date:
"NOME_PUBBL_LINEA" = "FOLLONICA - PIOMBINO";
"ORA_ARRIVA" = "1899-12-30T06:45:00";
"ORA_PARTE" = "1899-12-30T05:50:00";
I want to convert it like :
ora_arriva = 6.45,
ora_parte = 5.50
I have tried following :
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd'T'HH:mm:ss'+02:00'"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Italy/Rome"]];
NSDate *dte = [dateFormat dateFromString:dataDic[#"ORA_ARRIVA"]];
But it is giving me like
1899-12-30 00:51:40 +0000
for ora_arriva.
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'+02:00'"];
Just yyyy :)
dateFromString returns an NSDate. You need take that NSDate object and construct your time:
NSDate *myDate = [NSDate date];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comp = [gregorianCalendar components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:myDate];
NSLog(#"current time = %ld:%ld", [comp hour], [comp minute]);
Replace the myDate with your actual NSDate you got back from your dateFormatter
You need something like this
[dateformatter setDateFormat:#"MM-dd-yyyy hh:mm:ss"];
NSString *dateString = [dateformatter stringFromDate:adate];