I am working on a piece of code that is manipulating dates. Instances of the class DateFormatter have a property on them called generateCalendarDates. This is a boolean I believe is used to create a date based on the current calendar. However when I attempt to assign or inspect this property in Xcode I receive a description of the property from my code completion.
It actually tells me not to use this property. I was wondering why this is. Because I was actually thinking about using it.
The generatesCalendarDates property tells NSDateFormatter to create instances of NSCalendarDate instead of NSDate when it parses a date.
It's not available to you in Swift because NSCalendarDate is deprecated, and most deprecated APIs are not available in Swift.
So what was NSCalendarDate? It was a subclass of NSDate that added a timeZone property and had methods to perform calculations using the Gregorian calendar.
Why was it deprecated? Because Apple introduced the NSCalendar class in Mac OS X 10.4 (Tiger). You can use instances of NSCalendar to perform calculations using several different calendars, including the Gregorian. NSCalendar has its own timeZone property, where it makes sense; NSDate objects are always timezone-independent.
DateFormatter parses (and formats) dates using its calendar property. By default, that calendar property is set to Calendar.current, which is the system calendar. The user can choose the system calendar in Settings > General > Language & Region > Calendar.
You can set the calendar property explicitly to Gregorian if you want: dateFormatter.calendar = Calendar(identifier: .gregorian). But I wouldn't recommend it. If the user has chosen a different calendar, are you sure you want to force the use of Gregorian?
Related
In thingsboard I have a post-processing function for an server attribute update dialog.
update server attribute widget
post-processing date value
post-processing date value fails
I need to convert a text value (entered by the user in a widget) into a unix time stamp (milliseconds precision) to store it into an thingsboard attribute.
I also want do use this function to display the value in a formatted ISO date string. Something linke YYYY-MM-DD hh:mm:ss.
As I understand, the date.getMonth, getFullYear, ... functions are pretty standard for JavaScript. What do I need to do to use them in thingsboard too?
Is there a better way to convert dates?
You have to call use the new-operator to create a date-object.
See Date - JavaScript | MDN:
The only correct way to instantiate a new Date object is by using the new operator. If you simply call the Date object directly, such as now = Date(), the returned value is a string rather than a Date object.
So instead of
var date = Date(value);
it should be
var date = new Date(value);
However, there is a handy and popular javascript date-library called moment.js. Fortunately it is already bundled with Thingsboard and you can use it in widgets and those post-processing functions.
This question already has an answer here:
How to auto generate NSManagedObject subclasses with date attribute as Date instead of NSDate?
(1 answer)
Closed 5 years ago.
CoreData code generation doesn't generate NSManagedObject subsclass for swift3 for example: It creates NSDate properties instead of Date. Any idea how can I generate models for Swift3?
Note:- I have found nothing in CoreData Code Generation settings to switch language explicitly to Swift 3 yet we can generate Swift models. But there are some classes which have been updated in Swift 3.
A lot of property types still use subclasses of NSObject. You will most likely need to convert them manually.
For NSDate specifically you may use:
return (date as NSDate?) ?? nil
and
return (date as Date?) ?? nil
For easier usage you can create extensions on Date and NSDate to return the typecast values the way you want it. An alternative is to change the date fields in database to be something like rawDate and then create a property date that overrides both setter and getter and does the conversion to/from original property rawDate.
In any case these objects are not excluded in Swift 3, they are just replaced with newer types in most of the API.
I want to store dates in database as Normal Gregorian dates. However, I want to enable user view/edit them in Hijri calendar.
Now I have two problems:
Before displaying the date, convert it to Hijri (Done, using DisplayTemplates)
After user edit and submit, convert Hijri value to Gregorian. (My problem)
I now can use FormCollection and invoke a helper method to convert the received Hijri to Gregorian, but I am asking if there is some method to automatically convert the value to Gregorian, maybe something IModelBinder ??
I don't want breeze to tack on the time zone information on the server, or the client. How do I turn this off?
I have a SQL DATE field in the database- I don't want time. When working with DateTime variables in C#, I always want them set to midnight. I am overriding breeze.DataType.parseDateFromServer on the client as described here https://stackoverflow.com/a/17669486/2107571 but I seem to be still getting the adjustment on the server.
The key point here is that breeze is NOT tacking on time and timezone information. The issue is that dates automatically serialize with a time component because in javascript all dates have a time component. So even if this information is all set to all zeros, it will still be interpreted on the client as a date with a time component.
The second issue is that virtually every browser formats dates according to the browser's own timezone settings.
So the problem is either that the timezone of the server is different from that of the client or the server date is being serialized with no timezone information ( which will be interpreted by breeze as being a UTC date with a zero offset. i.e. which is very unlikely to match your local timezone.) This happens in .NET with server dates declared as DateTime as opposed to DateTime2 or DateTimeOffset.
To fix either of these you will need to replace Breeze's DataType.parseDateFromServer so that you completely ignore the timezone information coming in.
Something like:
breeze.DataType.parseDateFromServer = function (source) {
source = stripTime(source); // you will need to write the stripTime method.
return new Date(Date.parse(source));
};
If you use Moment.js, i think that you can do the following
breeze.DataType.parseDateFromServer = function (source) {
var date = moment(source);
return date.toDate();
};
I would like my app to load in the state of a NSDate object when is loaded and be able to save a new state set by a spinner.
My first dea was to get the NSdate object from the spinner then call descriptionWithLocale to get the date as a string, then save this string. But I could not see any methods to load a date as a string into a NSDAte.
Depending on your requirements, you have a couple of options. As Carl and rmaddy pointed out, NSDate conforms to NSCoding, which means it is serializable and can be easily stored in the NSUserDefaults. Here's a nice tutorial on using NSUserDefaults.
Alternatively, you can just store the date as an NSTimeInterval. In that case, you can use any of the following methods to store and retrieve the NSDate object (from NSDate class reference).
+ dateWithTimeIntervalSinceReferenceDate:
+ dateWithTimeIntervalSince1970:
– timeIntervalSinceReferenceDate
– timeIntervalSince1970
The dateWithTimeIntervalSince1970 and corresponding timeIntervalSince1970 methods are useful if you care about storing the date in epoch time.
Instead of converting NSData to NSString, better way is convert to NSTimeInterval (which is double) using NSDate -timeIntervalSince1970 method, then you can sore it in NSUserDefaults if you like.
If you really want save to file you can insert NSDate into NSDictionary object then save NSDictionary with -writeToFile:atomically: method (restore with +dictionaryWithContentsOfFile:)