Hi in my project I have Textfield as from and another is delay for ex in From filed I have value as "18:30"(string) and in delay filed "18" (integer) ,now I need to add these two value and should display value like "18:48"(string) in another textfield.If anyone know guide me thanks
Please refer to the following code for your query.
NSString *str=#"18:30";
int addTime=18;
NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm"];
NSDate *dateInput=[formatter dateFromString:str];
dateInput=[dateInput dateByAddingTimeInterval:addTime*60];
NSString *dateOutPut=[formatter stringFromDate:dateInput];
NSLog(#"OutputDate= %#",dateOutPut);
Console Output:
18:48
NSString *str=#"18:30";
int addTime=18;
NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm"];
NSDate *dateInput=[formatter dateFromString:str];
dateInput=[dateInput dateByAddingTimeInterval:addTime*60];
NSString *dateOutPut=[formatter stringFromDate:dateInput];
NSLog(#"OutputDate= %#",dateOutPut);
//convert string object into NSDate object
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm a"];
//Set System Timezone
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *TimeInDateFormat = [formatter dateFromString:#"12:45 AM"];
//Additing 01:00 Houes
NSTimeInterval secondsInOneHours = 1 * 60 * 60;
NSDate *dateOneHoursAhead = [TimeInDateFormat
dateByAddingTimeInterval:secondsInOneHours];
NSLog(#"after added dateOneHoursAhead: %#",dateOneHoursAhead);
abow code output is :
after added dateOneHoursAhead: 01:45 PM
try now........
Here is swift version.
let str: String = "10:30"
let addTime: Int = 30 //minutes
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
var dateInput: Date? = formatter.date(from: str)
dateInput = dateInput?.addingTimeInterval(TimeInterval(addTime*60))
endtime = formatter.string(from: dateInput!)
Thanks to #Andy Paul for objective c version of code.
Related
This is my input nsstring 30/01/18 3:25 PM
I want the date in this format : 30/Jan/18 3:25 PM.
This is what i tried :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter setDateFormat:#"dd/MM/yy HH:mm "];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:releaseDate];
[dateFormatter setDateFormat:#"dd/MMM/yy HH:mm "];
NSString *finalDate = [dateFormatter stringFromDate:date];
Also i tried this :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yy HH:mm z"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:releaseDate];
[dateFormatter setDateFormat:#"dd/MMM/yy HH:mm z"];
NSString *finalDate = [dateFormatter stringFromDate:date];
I am always getting date as nil !! What am i doing wrong?
Also : Time comes in both 12 hour format and 24 hour format. How do i handle both at the same time?
You check it below function, its working fine: -
-(void)DateChange
{
NSString *Input_Date =#"31/01/18 04:25 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/mm/yy hh:mm a"];
NSDate * Format_date = [dateFormatter dateFromString:Input_Date];
[dateFormatter setDateFormat:#"dd/MMM/yy hh:mm a"];
NSString *Change_date = [dateFormatter stringFromDate:Format_date];
NSLog(#"Final Change Date :- %#",Change_date);
}
My Output is :-
Final Change Date :- 31/Jan/18 04:25 AM
Use this function written in Swift to change the date format. Its working not
func dateFormatterfromString(_ date: String!) -> String{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MMM-yyyy"
let date = dateFormatter.date(from:date)!
dateFormatter.dateFormat = "yyyy-MM-dd"
let dateString = dateFormatter.string(from:date)
return dateString
}
I have a UIDataPicker in my viewController with default location, when my user finishes selecting the date I run this code:
NSString *dateString = [NSDateFormatter localizedStringFromDate:[self.dataPicker date]
dateStyle:NSDateFormatterLongStyle
timeStyle:NSDateFormatterNoStyle];
With that code I can storage the date in the following format:
May 31, 2016
Later in my code I need to convert this string into a real date format, for this I use the code below:
-(NSDate*)convertStringToDate:(NSString*)date{
NSString *dateString = date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
return [dateFormatter dateFromString:dateString];
}
But this code return a null value. As the datepicker is set by default, my system can receive any date format, but in the end I want it to be converted to the format en_us.
How I can solve this problem?
Don't store the date as a string; store it as an offset, in seconds, from some reference date.
i.e:
uint64_t offset = (uint64_t)[[self.dataPicker date] timeIntervalSinceReferenceDate];
// store this 64-bit unsigned integer.
This takes less space and is quicker to convert to/from an NSDate object.
You can leave the offset as an NSTimeInterval (64-bit floating point double) if you prefer, but as you aren't storing date & time, uint64_t should do...
Use this code,
-(NSDate*)convertStringToDate:(NSString*)date{
NSString *dateString = date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setDateFormat:#"MMM d, yyyy"];
return [dateFormatter dateFromString:dateString];
}
hope its helpful
The formatting string depends on the locale you are using. From the localizedStringFromDate documentation:
Returns string representation of a given date formatted for the
current locale using the specified date and time styles.
This method uses a date formatter configured with the current default
settings. The returned string is the same as if you configured and
used a date formatter as shown in the following example:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.formatterBehavior = NSDateFormatterBehavior10_4;
formatter.dateStyle = dateStyle; formatter.timeStyle = timeStyle;
NSString *result = [formatter stringForObjectValue:date];
Means, you should do the next:
-(NSDate*)convertStringToDate:(NSString*)dateString {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.formatterBehavior = NSDateFormatterBehavior10_4;
dateFormatter.dateStyle = NSDateFormatterLongStyle;
dateFormatter.timeStyle = NSDateFormatterNoStyle;
return [dateFormatter dateFromString:dateString];
}
I know that the NSDateformatter suite of functionality is a boon for mankind, but at the same time it is very confusing to me. I hope you can help me out.
Somewhere in my code, there is an int representing a month. So: 1 would be January, 2 February, etc.
In my user interface, I would like to display this integer as proper month name. Moreover, it should adhere to the locale of the device.
Thank you for your insights
In the mean time, I have done the following:
int monthNumber = 11
NSString * dateString = [NSString stringWithFormat: #"%d", monthNumber];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM"];
NSDate* myDate = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMMM"];
NSString *stringFromDate = [formatter stringFromDate:myDate];
[formatter release];
is this the way to do it? It seems a bit wordy.
Another option is to use the monthSymbols method:
int monthNumber = 11; //November
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
NSString *monthName = [[df monthSymbols] objectAtIndex:(monthNumber-1)];
Note that you'll need to subtract 1 from your 1..12 monthNumber since monthSymbols is zero-based.
let monthName = DateFormatter().monthSymbols[monthNumber - 1]
You can change the dateFormat of the NSDateFormatter. So to simplify your code:
int monthNumber = 11
NSString * dateString = [NSString stringWithFormat: #"%d", monthNumber];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM"];
NSDate* myDate = [dateFormatter dateFromString:dateString];
[formatter setDateFormat:#"MMMM"];
NSString *stringFromDate = [formatter stringFromDate:myDate];
[dateFormatter release];
You should also set the locale once you init the date formatter.
dateFormatter.locale = [NSLocale currentLocale]; // Or any other locale
Hope this helps
Best solution for this is , standaloneMonthSymbols method,
-(NSString*)MonthNameString:(int)monthNumber
{
NSDateFormatter *formate = [NSDateFormatter new];
NSArray *monthNames = [formate standaloneMonthSymbols];
NSString *monthName = [monthNames objectAtIndex:(monthNumber - 1)];
return monthName;
}
How about:
NSUInteger i = <your month integer>;
NSDateFormatter *df = [NSDateFormatter new];
// change locale if the standard is not what you want
NSArray *monthNames = [df standaloneMonthSymbols];
NSString *monthName = [monthNames objectAtIndex:(i - 1)];
[df release];
Both answers from Anna Karenina and Carl doesn't work that well as they won't return month name in nominativ for some cultures. I suggest to use the proposed solution from Pascal, which solves this issue (by replacing monthSymbols with standaloneMonthSymbols)
In Swift 3.0
let monthNumber = 3
let fmt = DateFormatter()
fmt.dateFormat = "MM"
let month = fmt.monthSymbols[monthNumber - 1]
print(month)
// result
"March\n"
Swift 4.X
print((DateFormatter().monthSymbols[month-1].capitalized)) //month is int less than 12
For Example:
print((DateFormatter().monthSymbols[11-1].capitalized))
Output
November
NSDate to NSString -> As Dateformat Ex: 2015/06/24
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
[dateformate setDateFormat: #"yyyy/MM/dd"];
NSString *date = [dateformate stringFromDate:selectedDate]; // Convert date to string
NSDate to NSString -> As Dateformat Ex: 2015 June 24, 1:02 PM
[dateformate setDateFormat:#"yyyy MMMM dd, h:mm a"];
NSString *displayDate = [dateformate stringFromDate:selectedDate]; // Convert date to string
NSLog(#"date :%#",date);
NSLog(#"Display time = %#", displayDate);
And with ARC :
+ (NSString *)monthNameFromDate:(NSDate *)date {
if (!date) return #"n/a";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM"];
return [[df monthSymbols] objectAtIndex:([[df stringFromDate:date] integerValue] - 1)];
}
You should be able to get rid of the release and re-allocation of the dateFormatter, cutting out a couple of lines, but that's all I see.
hi i have problem while converting Date Formats...
the date time is in the Format 12 hour Format like 12/9/2010 4:00:00 PM
(Month/date/year Hour:Min:sec Pm)
i need to Convert it into 24 hour format like 12/9/2010 16:00:00
Can any one Help me please....
Thx in advance
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateformatter.dateFormat = #"hh:mm a";
NSDate *date = [dateformatter dateFromString:departTime];
dateformatter.dateFormat = #"HH:mm";
NSString *time24 = [dateformatter stringFromDate:date];
departTimelbl.text=time24;
Simply do This to get 24 hour time format (In Swift 2)
let formatter = NSDateFormatter()
formatter.dateFormat = "HH:mm a" //(hh:mm a----> for 12 hour format)
let timeString = formatter.stringFromDate(NSDate())
print(timeString)
UPDATE: Swift 3 version
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm a" //(hh:mm a----> for 12 hour format)
let timeString = formatter.string(from: Date())
print(timeString)
You need to use a NSDateFormatter. I encourage you to read the class reference as linked by Kay.
Here's how you'd do it. I'm assuming the date is originating as a NSString.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM-dd-yyyy HH:mm:ss"];
NSDate *myDate = [df dateFromString:yourNSString];
NSString *myNewDate = [formatter stringFromDate:myDate];
[formatter release];
[myDate release];
Use these functions,this will change the date string from 12 to 24 hr formate and 24 to 12hr formate.
-(NSString *)changeformate_string24hr:(NSString *)date
{
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate* wakeTime = [df dateFromString:date];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
return [df stringFromDate:wakeTime];
}
-(NSString *)changeformate_string12hr:(NSString *)date
{
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
NSDate* wakeTime = [df dateFromString:date];
[df setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
return [df stringFromDate:wakeTime];
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"HH:mm";
NSDate *date = [dateFormatter dateFromString:#"15:15"];
dateFormatter.dateFormat = #"hh:mm a";
NSString *pmamDateString = [dateFormatter stringFromDate:date];
Take a look at NSDateFormatter class reference and the data formatting guide. There you will find more especially the method dateFromString and stringFromDate are interesting for you. There is another question dealing with similar problem From string with locale to date on iphone sdk where you can find some sample code when you want to set a special formatting template string.
In Swift
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy hh:mm:ss a"
var date: Date? = dateFormatter.date(from: "12/9/2010 4:00:00 PM")
dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss"
var convertedString: String? = nil
if let aDate = date {
convertedString = dateFormatter.string(from: aDate)
}
In Objective C
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"MM/dd/yyyy hh:mm:ss a";
NSDate *date = [dateFormatter dateFromString:#"12/9/2010 4:00:00 PM"];
dateFormatter.dateFormat = #"MM/dd/yyyy HH:mm:ss";
NSString *convertedString = [dateFormatter stringFromDate:date];
I have weird result trying to output NSDate object from NSString.
My NSString is: 1976-06-11
My method to convert is:
-(NSDate*)dateFromString:(NSString *)dateString{
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormat dateFromString:dateString];
return date;
}
But it output 1976-06-10 21:00:00 +0000
How could that happen? Difference in 1 day.
You have date in UTC format. Use this code to converting your date to local time:
NSTimeInterval seconds; // assume this exists
NSDate *ts_utc = [NSDate dateWithTimeIntervalSince1970:seconds];
NSDateFormatter *utcFormatter = [[NSDateFormatter alloc] init];
utcFormatter.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
utcFormatter.dateFormat = #"yyyy.MM.dd G 'at' HH:mm:ss zzz";
NSDateFormatter *localFormatter = [[NSDateFormatter alloc] init];
localFormatter.timeZone = [NSTimeZone timeZoneWithName:#"EST"];
localFormatter.dateFormat = #"yyyy.MM.dd G 'at' HH:mm:ss zzz";
NSString *utcDateString = [utcFormatter stringFromDate:ts_utc];
NSString *LocalDateString = [localFormatter stringFromDate:ts_utc];
Or you can use [NSTimeZone defaultTimeZone] to prevent hardcoded strings for timezone names. This method returns the system time zone, If no default time zone has been set.
You can use following methods to convert an UTC date string into UTC date and local date
- (NSDate *)convertIntoGMTZoneDate:(NSString *)dateString
{
NSDateFormatter *gmtFormatter = [[NSDateFormatter alloc]init];
[gmtFormatter setDateStyle:NSDateFormatterFullStyle];
[gmtFormatter setTimeStyle:NSDateFormatterFullStyle];
[gmtFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
return [gmtFormatter dateFromString:dateString];
}
- (NSDate *)convertIntoSystemZoneDate:(NSString *)dateString
{
NSDateFormatter *systemZoneFormatter = [[NSDateFormatter alloc]init];
[systemZoneFormatter setDateStyle:NSDateFormatterFullStyle];
[systemZoneFormatter setTimeStyle:NSDateFormatterFullStyle];
[systemZoneFormatter setTimeZone:[NSTimeZone systemTimeZone]];
return [systemZoneFormatter dateFromString:dateString];
}
func dateFromString(dateString: String) -> NSDate {
// Convert string to date object
var dateFormat = NSDateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd"
dateFormat.timeZone = NSTimeZone(name: "UTC")
var date = dateFormat.dateFromString(dateString)!
print(date)
return date
}
output :
1976-06-11 00:00:00 +0000
If you debug code, it shows 1 day difference but after run you will find the actual date which is you enter.
It works for me.I think it will helps you.
Thank you