I want to add date into my database ..
and I want to add it in some specific format ..like dd/mm/yyyy
how to check the validation for it?
Like e-mail validation .. I want to check is it in specified format or not?
how to do this ?
1) What is the input method for your date?
If it is thru a UIDatePicker, you have nothing to do, the UIDatePicker will return a valid NSDate. I encourage you to manipulate NSDate objects and use UIDatePickers as date inputs especially to avoid the question of date validation.
If it is thru a text field, you can set the UITextField's inputView to an UIDatePicker and you won't have to bother about date validation either as you will obviously directly have a (parsed) NSDate that you can manipulate directly, being sure it is valid.
Then in either case, use NSDateFormatter to convert your NSDate (entered by the user thru UIDatePicker) to an NSString (to insert in your database).
2) If you have a plain text entered by the user thru the keyboard, and can't or don't want to use a UIDatePicker (even as the inputView of your UITextField), you can convert your NSString to an NSDate thru an NSDateFormatter to check if it returns a non-nil NSDate (meaning it is a valid date).
Note: You may also use NSRegularExpression or NSPredicate classes to check the format of your string... but you won't have the subtleties of NSDate validation (like checking that 31/02/2011 or 67/13/99 is invalid)
Related
I'm trying to apply a filter on realm which includes a Date but with no luck.
I've found out that Date object cannot be passed because the %# format expects a Foundation object as argument, so I've applied a cast to NSDate.
let newDate = Date()
realm.objects(E.self)
.filter(String(format: "%# <= %#", key, newDate as NSDate)).first
The issue that appears is "Unable to parse the format string timestamp==2020-03-20 08:21:00 +0000"
key is the name of the field, which in this case it is "timestamp" and on the model it has the type Date.
Any input is appreciated.
Thanks
You should use:
.filter("\(key) <= %#", newDate as NSDate)
This is calling the overload of Realm's filter method that accepts a format and arguments.
String(format:) is not the right thing to use here, as that just does general string formatting. But here, you want the date to be formatted according to the rules of NSPredicate formats. On the other hand, the key can just be interpolated into the string because the name of the table column doesn't need a special format.
If key comes from a UITextField or something like that, then you might need to beware of injection attacks as well, and validate and/or escape the key properly beforehand.
i'm be able to retrieve the region format of the user, ad example it_IT with this code
[NSLocale currentLocale] objectForKey:NSLocaleIdentifier];
is it possible have the complete list of these identifier?
(it_IT, de_DE, de_AT,....)
Thanks
[NSLocale availableLocaleIdentifiers] is yet another way.
For testing, specifically to insure all date formats are handled properly in my UI, I rip through that array, set a NSDateFormatter to use that and test for every possible combination of date formatting in an automated way.
Interesting what you learn along the way as well when you see some date representations that break your assumptions about how some dates, day names, etc might be formatted.
Try with this:
Returns an array of NSString objects that represents all known legal ISO currency codes.
[NSLocale ISOCurrencyCodes];
Returns an array of NSString objects that represents all known legal country codes.
[NSLocal ISOCountryCodes];
Returns an array of NSString objects that represents all known legal ISO language codes.
[NSLocale ISOLanguageCodes];
All the info from:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSLocale_Class/Reference/Reference.html
I am adding dates into dictionary as the key and the corresponding value as some text ,but I am converting the date into string ,before adding to a dictionary,I have written the below code
[dateNoteDict setValue:datestring forKey:notes.text];
but before adding to dictionary I want to make some date comparision such that ,if the date present in dictionary is greater than ,the nest date then ,I should add below in the dictionary and so..on..
But I am not understanding how to do that.
So friends,please help me out..
Regards
Ranjit
You should do the comparison before converting the dates into NSStrings.
Assuming you are using NSDate objects, you can use the compare method. Have a look at this answer from S.O about date comparison.
Set some particular date format while setting as key in your dictionary. You can use NSDateFormatter for this. Also convert new date in the same format and then use "compare" for date comparison.
Thanks
I am trying to format a date as it appears in iOS calendar application.
The format is the NSDateFormatterMediumStyle and displays a date such as "Nov 27, 2011”.
Documentation
However such a format is not available in the .NET Framework date formats, so I have to use the NSDateFormatter to format the date.
Documentation
The problem I have is that in Monotouch I cannot find a method in NSDateFormatter to format the date.
In the Apple examples there is a [dateFormatter stringFromDate:date]; method, however it looks that this method is missing in Monotouch or there is an alternative?
Jason's comment makes a great point about using the Rosetta. It shows that the stringFromDate:date selector is binder inside the NSDateFormatter type.
note: since a lot of the base (shared between MonoTouch and MonoMac) code is open source you can also find the binding sources on github.
From the above I assume you should be able to get the results you're looking for (i.e. iOS, not .NET, culture-aware date/time strings) by doing:
var f = new NSDateFormatter ();
f.DateStyle = NSDateFormatterStyle.Medium;
string result = f.ToString (nsdate);
.NET supports custom date formats. This should do what you want:
myDate.ToString("MMM d, yyy");
I am in the unfortunate position that I need to use a composite id in a Grails app where I work with legacy data. This means I have to override some actions in the controller, but as I did this I was struck by the fact that I could not use use a date argument directly as a parameter to a dynamic method.
Instead of just doing MyLegacyObj.findBySystemIdAndLogDate(params.systemId, params.logDate), I first needed to parse the date string before giving it to the dynamic method. To further complicate matters I had no idea what format the date string had (until I added lots of log.debug() string to the output). So now I have a bit of code looking like this
def formatter = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy")
MyLegacyObj.findBySystemIdAndLogDate(params.systemId, formatter.parse(params.logDate));
This feels unoptimal, no to say dangerous (what if the date format changes with the locale?)? What would be a recommended way of doing this, and do I really need to parse dates at all?
Date is a pretty complex object and params are just Strings, so Date is submitted in parts. It is "magically" assembled from the parts when assigning x.properties = params.
Command object will do the work for you, if you add a Date field to it.
It has nothing to do with methods' dynamic or static invocation. Your GSP that renders Date editor might interfere too.