如何在我的代码上转换日期... [英] how to convert date on my code...

查看:127
本文介绍了如何在我的代码上转换日期...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

i我在mvc中使用jqgrid。

i希望将我的日期转换成我桌子上的波斯日期。

i我正在使用这个这是我的模型代码:

hello
i am working with jqgrid in mvc.
i want to convert my date to persian date in my table.
i am using this code for that.
this is my model code:

  public OrderModel()
        {
            OrdersGrid = new JQGrid
            {
                Columns = new List<JQGridColumn>()
                                 {
                                     new JQGridColumn { DataField = "OrderID", 
                                                        // always set PrimaryKey for Add,Edit,Delete operations
                                                        // if not set, the first column will be assumed as primary key
                                                        PrimaryKey = true,
                                                        Editable = false,
                                                        Width = 50 },
                                     new JQGridColumn { DataField = "OrderDate", 
                                                        Editable = true,
                                                        Width = 100, 
                                                        DataType = typeof(DateTime),
                                                        DataFormatString = "{0:d}",
                                                        

                                                        
                                     },
                                     new JQGridColumn { DataField = "CustomerID", 
                                                        Editable = true,
                                                        Width = 100 },
                                     new JQGridColumn { DataField = "Freight", 
                                                        Editable = true,
                                                        Width = 75 },
                                     new JQGridColumn { DataField = "ShipName",
                                                        Editable =  true,
},
                                     new JQGridColumn { DataField = "ShipAddress",
                                                        Editable =  true,
                                                      }                                     
                                 },
                Width = Unit.Pixel(1000),
                Height = Unit.Pixel(400)
            };

            OrdersGrid.ToolBarSettings.ShowRefreshButton = true;
        }

        public JQGrid OrdersGrid { get; set; }
    }
}





我的转换代码:



my conversion code :

public static string ToPersianDate(this DateTime date)
        {
            var dateTime = new DateTime(date.Year, date.Month, date.Day, new GregorianCalendar());
            var persianCalendar = new PersianCalendar();
            return persianCalendar.GetYear(dateTime) + "/" +
                   persianCalendar.GetMonth(dateTime).ToString("00") + "/" +
                   persianCalendar.GetDayOfMonth(dateTime).ToString("00");
        }



这是我的服务器端代码:


and this is my server side code :

        public ActionResult GridDemo()
        {
   
            var gridModel = new OrderModel();
            var ordersGrid = gridModel.OrdersGrid;
        
            S/etUpGrid(ordersGrid);

          //my code for convert date...

            var list = new List<OrderModel>();
            
    NorthWindsDataContext ss = new NorthWindsDataContext();

            gridModel = (list.Select(t => new
            {

                OrderID = t.OrdersGrid.OrderID,
                OrderDate = t.OrderDate == null
                    ? ""
                    : new PersianDateTime(t.OrderDate.Value)
                        .ToString(PersianDateTimeFormat.DateShortTime)

            }))
            .ToList();



and so Other code for convert code ...

                   //OrdersGrid = new{product => new JQGrid
                   // {
                    
                   //     product.Id.ToString(CultureInfo.InvariantCulture),
                   //     product.Name,
                   //     product.DateTimeAdDateTime.ToPersianDate(),
                   //     product.Price.ToString(CultureInfo.InvariantCulture)
                    
                   // }

           // };

            // Pass the custmomized grid model to the View
            

            return View(gridModel);
        }





但它们不正确且无法检测到OrderDate。



如何修改我的代码...

请给我一个示例代码。



非常感谢你来自答案。



but they are not correct and that does not detect OrderDate.

how shoud i modify my code...
pls give me a sample code.

thank you so much from answer.

推荐答案

一种可能的解决方案是:在数据库内部,使用常规时间数据类型,例如 DATE 。对于演示文稿,在应用程序中,通过类使用波斯日历 System.Globalization.PersianCalendar

https://msdn.microsoft.com/en-us/library/system.globalization.persiancalendar% 28v = vs.110%29.aspx [ ^ ]。



每次从数据库获取时间和需要时,您都可以转换时间结构在波斯日历中表示它,或者当您在波斯日历中获得用户输入并想要添加/修改数据库数据时。时间本身的概念是文化中立的;你可以随时代表不同文化相关系统中的类型。



-SA
One possible solution would be this: internally, in the database, use regular time data types, such as DATE. For presentations, in the applications, use Persian calendar through the class System.Globalization.PersianCalendar:
https://msdn.microsoft.com/en-us/library/system.globalization.persiancalendar%28v=vs.110%29.aspx[^].

You could convert the time structures everytime you get time from database and need to represent it in Persian calendar, or when you get user input in Persian calendar and want to add/modify database data. The concept of time itself is culture-neutral; you can always represent type in different culture-dependent systems.

—SA


替换

Replace
gridModel = (list





with



with

ordersGrid.Columns = (list





问题是:你将列表分配给OrdersGrid对象,需要将列表分配给OrdersGrid对象上的Columns属性(List< jqgridcolumn>),这是OrderModel对象的属性。



The problem is: you're assigning the list to the OrdersGrid object, the list needs to be assigned to the Columns property (which is a List<jqgridcolumn>) on the OrdersGrid object, a property of the OrderModel object.


这篇关于如何在我的代码上转换日期...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆