Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Persian Calender in MVC , Asp.net

I use a DateTime variable in MVC and I want to show Persian calender for

@Html.EditorFor(x=> x.ProductionDate)

How can I do it?

like image 889
Ahmad Avatar asked Apr 27 '13 06:04

Ahmad


2 Answers

I found the solution

1.Go to www.amib.ir/weblog/?page_id=316 and download the latest version of "AMIB_jsPersianCal"


2.Add "js-persian-cal.min.js" and "js-persian-cal.css" and "pcal.png" to your project
you can change the css for specified the Url of PNG file

3.Add the css and js file to your cshtml file

<link href="@Url.Content("~/Content/js-persian-cal.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/js-persian-cal.min.js")"></script>


4.change your date file like

@Html.TextBoxFor(m => m.Birthdate, new { @id = "pcal1", @class = "pdate" });

note: I use string variable for Birthdate


5.add the script to the end of cshtml File

<script type="text/javascript">
var objCal1 = new AMIB.persianCalendar('pcal1'); </script>
like image 196
Ahmad Avatar answered Nov 08 '22 21:11

Ahmad


Try this template (datetime.cshtml) to show/convert DateTime to PerisanDate automatically:

@*
  Copy this file to:
  Views\Shared\DisplayTemplates\datetime.cshtml
*@

@using System.Globalization
@model Nullable<DateTime>

@helper ShamsiDateTime(DateTime info, string separator = "/", bool includeHourMinute = true)
{
    int ym = info.Year;
    int mm = info.Month;
    int dm = info.Day;
    var sss = new PersianCalendar();
    int ys = sss.GetYear(new DateTime(ym, mm, dm, new GregorianCalendar()));
    int ms = sss.GetMonth(new DateTime(ym, mm, dm, new GregorianCalendar()));
    int ds = sss.GetDayOfMonth(new DateTime(ym, mm, dm, new GregorianCalendar()));    
    if (includeHourMinute)
    {
        @(ys + separator + ms.ToString("00") + separator + ds.ToString("00") + " " + info.Hour + ":" + info.Minute)
    }
    else
    {
        @(ys + separator + ms.ToString("00") + separator + ds.ToString("00"))
    }
}

@if (@Model.HasValue)
{
  @ShamsiDateTime(@Model.Value , separator: "/", includeHourMinute: false)
}

Also if you are looking for the Persian date time pickers, take a look at: http://www.dotnettips.info/newsarchive/details/1122

like image 34
VahidN Avatar answered Nov 08 '22 21:11

VahidN