MVC action parameter convert date - asp.net-mvc

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 ??

Related

What are the JavaScript functions supported in post-processing function

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.

Why can't I use DateFormatter property generateCalendarDates?

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?

getting wrong NSDate and Formate Using NSDateFormatter (datefromString)

Hi, I am using the below code in my project.
I am posting the notification from one view to another view. passing
string(date) as parameter of Nsnotifiationcenter,but i am not able to
covert passed date into original date format I require.

In MVC,using Json to transfer object with time formate property,How to avoid reduce 8 hours automatically?

A student class has a property called birthday. My web Server is in Beijing (UTC+8), and when I get data from the server in JSON format, the birthday property always reduces 8 hours to the original date, How can I correct this? I am also in Beijing and I add 8 hours to all birthday properties, but if I am in another timezone or the web server is not in Beijing then I have to add other hours manually.
On the web server, here's my code:
return Json(student);
The codes that get the data:
var studentReader= new StreamReader(Request.InputStream);
var student= JsonHelper.FromJson<Student>(studentReader.ReadToEnd());
The JsonHelper class:
public static T FromJson<T>(this string json)
{
return JsonConvert.DeserializeObject<T>(json);
}
The recommendation is that, you should store all datetime values in UTC. In the client code, you can convert it to any timezone you want.
Your JSON serialization is fine. No problems there.
You need to make sure to store all date-time values as UTC. And when you display the dates then you can make the conversion to the Timezone you need.
Here's how I usually do it:
Every time I store a DateTime I use DateTime.ToUniversalTime() to make sure the date is stored on UTC on the database.
When I need to print the date in a View I have a method that I used that takes a specific TimeZone and converts the date. See bellow for a code example.
I never rely on Client-Side zones. If I have a broad range of users from many geographical points I just make then choose their preferred timezone and store this information on their profiles (Google, Microsoft, and 37Signals all do this for their web applications just to name a few examples).
public static DateTime ToLocalTimeZone(this object date)
{
DateTime dt;
if (!DateTime.TryParse(date.ToString(), out dt))
return DateTime.UtcNow;
//Timezone string. This can be stored on web.config
//or on the user profile as well
var timeZone = "Pacific SA Standard Time";
var defaultTimeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZone);
var userTime = TimeZoneInfo.ConvertTimeFromUtc(dt, defaultTimeZone);
return userTime;
}
More info on previous questions here and here
Edit: List of allowed Timezone strings here

Why is my date being populated wrong?

I have an entity with a datetime object. The database time is 2012-05-07 00:00:00.000 and the data from the breeze web api call is coming as 2012-05-07T00:00:00.000, but the property holding the data is Sun May 06 2012 20:00:00 GMT-0400 (Eastern Daylight Time). Note the 4 hour difference.
According to this:
[T]he timezone of the value on the server will be carried over to the value on the client (and vice versa).
It sounds like Breeze is treating the datetime as UTC and converting it to EDT. Is Breeze doing so based on the lack of timezone information from the original date? For this specific instance I do not care about Timezone, how do I get the actual date from the database regardless of client/server timezone?
You can replace Breeze's DataType.parseDateFromServer to NOT infer any time zone info if it is not provided:
breeze.DataType.parseDateFromServer = function (source) {
return new Date(Date.parse(source));
};
However, there is another problem you are likely to run into with this. Different browsers interpret DateTime strings without a time zone offset differently... So you may still get strange results depending on the browser. If that happens you will need to add some browser detection code to the snippet above.
EDIT: The suggestion was also made that if you use Moment.js you can do the following
breeze.DataType.parseDateFromServer = function (source) {
var date = moment(source);
return date.toDate();
};

Resources