Format the date in the. net view. net view date

Source: Internet
Author: User

Format the date in the. net view. net view date

Yesterday, I made a function to display a time in the interface according to the specified format. If the time is formatted directly in expression (as follows :)

  

@ Html. displayFor (c => Convert. toDateTime (c. issueDate ). toString ("yyyy-MM-dd HH: mm: ss"), new {@ class = "form-control", onFocus = "WdatePicker ({dateFmt: 'yyyy-MM-dd HH: mm: ss', alwaysUseStartDate: true}) ", @ placeholder =" Date of Issue "})

In the above cases, access denied will be reported during execution. Of course this is not difficult. There are several solutions (my front-end time control uses My97Datepicker ):

(Ps. ignore the non-bound Model scenario here, because if the Model is not bound, the time is only displayed. You can use ToString (string format) to format the time format; at the same time, you can also use the abbreviation of format, format can refer to: http://www.cnblogs.com/shaocm/archive/2012/08/15/2639998.html)

First, the simplest thing is to directly format the field in the background using ToString (string format) in the specified format, and then bind it to the foreground, because I use AutoMap to directly map database values to this Model, the method seems awkward, so this method is not needed.

Second, if you only need to display on the interface, it is not editable, for example:

    

@ Html. displayFor (c => c. issueDate, new {@ class = "form-control", onFocus = "WdatePicker ({dateFmt: 'yyyy-MM-dd HH: mm: ss', alwaysUseStartDate: true }) ", @ placeholder =" Date of Issue "})

 

At this time, you can directly Add the following annotation to the Model attribute:

        /// <summary>        /// IssueDate        /// </summary>                 [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd HH:mm:ss}")]        public DateTime? IssueDate { get; set; }

In this way, you can directly control the date display format of this attribute through annotations. Note that if the view interface is not using @ Html. displayFor, but @ Html. textBoxFor or @ Html. editorFor, This annotation format does not work.

Third, if @ Html. EditorFor is used on the interface, you can add another annotation to control the display format of the interface, as shown below:

    

        /// <summary>        /// IssueDate        /// </summary>                 [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd HH:mm:ss}")]        public DateTime? IssueDate { get; set; }

Then use:

@ Html. edittorFor (c => c. issueDate, new {@ class = "form-control", onFocus = "WdatePicker ({dateFmt: 'yyyy-MM-dd HH: mm: ss', alwaysUseStartDate: true }) ", @ placeholder =" Date of Issue "})

In this way, you can use this annotation to control the date display format.

Fourth, if @ Html. TextboxFor is required to be displayed on the interface, as follows:

    

@ Html. textBoxFor (m => m. remittanceDate, new {@ class = "form-control validate [required]", onFocus = "WdatePicker ({startDate: '% y-% M-01', dateFmt: 'yyyy-MM-dd', alwaysUseStartDate: true}) ", @ placeholder =" Enter remittance date "})

 

, The format control of the preceding two annotations does not work. At this time, there are also three solutions:

① Use the overload function of @ Html. TextBoxFor, one of which has the string format parameter, as follows:

      

@ Html. textBoxFor (m => m. remittanceDate, "{0: yyyy-MM-dd}", new {@ class = "form-control validate [required]", onFocus = "WdatePicker ({startDate: '% y-% M-01', dateFmt: 'yyyy-MM-dd', alwaysUseStartDate: true}) ", @ placeholder =" Enter remittance date "})

② Use the following methods:

    

@ Html. textBoxFor (m => m. remittanceDate, new {@ class = "form-control validate [required]", Value = String. format ("{0: yyyy-MM-dd}", Model. remittanceDate), onFocus = "WdatePicker ({startDate: '% y-%', dateFmt: 'yyyy-MM-dd', alwaysUseStartDate: true })", @ placeholder = "Enter the remittance date "})

③ The following is a similar method:

    

@ Html. textBoxFor (c => c. issueDate, new {@ class = "form-control", Value = Convert. toDateTime (Model. shipBeginTime ). toString ("yyyy-MM-dd HH: mm: ss"), onFocus = "WdatePicker ({dateFmt: 'yyyy-MM-dd HH: mm: ss', alwaysUseStartDate: true}) ", @ placeholder =" Date of Issue "})

Note that the Convert must be added to the method in ③. toDateTime () is converted to the time type. Otherwise, the ToString (string format) function will report that there is no overload with a parameter, because the ToString () function is a common function that accumulates the Object, this method is reloaded for the DateTime type,

The ToString () method of the DateTime type can contain one parameter or not, so you must convert it to the DateTime type to use the ToString (string format) function for formatting.

 

The above uses MS Mvc. If it is a traditional WebForm, use <% # Eval ("PA_STATUS") %> to bind the value, Eval () there are also overload functions with formatting parameters to control the display format, but WebForm I remember sometimes to use Eval, sometimes to use DataBind ()

I forgot about the function. If you have any idea, please let me know in the comments. Thank you!

 

 

 

 

 

 

 

 

 

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.