Search code examples
asp.net-mvctelerik-mvc

Show 0001 year in Telerik grid column from extensions for ASP.NET MVC


Telerik extensions for ASP.NET MVC ( click ). Our app widely uses date like 01/01/0001 (DateTime.MinValue). Please don't ask "why not nullable?". Yeap, you are right, but there are a lot of work to rewrite all the stuff, so we need a temporary solution.

Currently date like '01/01/0001' is shown as '01/01/1' in the column below:

columns.Bound(p => p.EffectiveSince).Width(50).Format("{0:MM/dd/yyyy}");

If click click 'Edit' at some row and just press 'Update' w/o any modifications, then such date ('01/01/1') is saved as 01/01/2001, which breaks the logic. But if manually type '01/01/0001' - it is saved as expected. So, telerik "eats" leading zeros.

Could you suggest a solution of this "problem", how to display "01/01/0001" instead of "01/01/1" ? (yes, I know about nullable, another solution..)


Solution

  • Found the reason, telerik ate leading zeros. Just beatify telerik.common.min.js and search for the string "yyyy: aO". aO is year returned by getFullYear(). That's why all the dates like '01/01/0001' are converted to 01/01/1, which cause to problems with displaying / sending / saving to db such dates. Telerik's formatString function displays year in format 'yyyy' in another way vs C# string.Format. The fix is easy. Just change

    yyyy: aO // 0001 year == 1

    to

    yyyy: ('0000' + aO).slice(-4) // 0001 year = 0001