0

I am trying to render DateTime in format DD/MM/YYYYTHH:mm in C# ASP.NET CORE 5.0 MVC but I am not sure is it good idea to render in backend side or in frontend. Since in frontend side I use DataTable Currently this is format which I get in my datatable

2021-03-05T13:22:48.3190414  

But I want to be formated as

05-03-2021T13:22

I try couple of solution something like in datatable

{
                "data": "dateAndTime", "width": "15%",
                "def": function () { return new Date(); },
                "displayFormat": 'DD/MM/YYYY',
                "wireFormat": 'YYYY-MM-DD',
                "keyInput": false
            },

 [Display(Name = "Datum i vrijeme slanja")]
        public DateTime DateAndTime { get; set; } = DateTime.Now;

Also in backend I try something like this:

ticketVM.Ticket.DateAndTime = DateTime.Parse("DD/MM/YYYY");

And get this kind of error

The string 'DD/MM/YYYY' was not recognized as a valid DateTime. There is an unknown word starting at index '0'.

Any advice and suggestion how to get format render in format DD/MM/YYYYTHH:mm ??

6
  • String '13/12/2021' was not recognized as a valid DateTime. Commented Mar 5, 2021 at 12:45
  • You need to parse the format that the string actually has. If you want to lose the millis: set them to 0. If you then want to display another format, you need to format the datetime value accordingly. DateTime itself has no format - it's just a number. Commented Mar 5, 2021 at 12:49
  • How to format DateTIme in format DD/MM/YYYYTHH:mm. You mean format in Model by using ` [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy hh:mm}")]` Commented Mar 5, 2021 at 12:52
  • dotnetfiddle.net/gK6fTz , Your format is incorrect. See Custom date and time format strings. It needs to be "dd/MM/yyyyTHH:mm". Commented Mar 5, 2021 at 12:55
  • ^^ Example with parsing: dotnetfiddle.net/cdenxp Commented Mar 5, 2021 at 13:01

1 Answer 1

1

The Format String means it's a string after format:

public string DateAndTime()
{
    return DateTime.Now.ToString("dd/MM/yyyy HH:mm");
}

You can replace :

[Display(Name = "Datum i vrijeme slanja")]
public DateTime DateAndTime { get; set; } = DateTime.Now;

to

[Display(Name = "Datum i vrijeme slanja")]
public string DateAndTime { get; set; } = DateTime.Now.ToString("dd/MM/yyyy HH:mm");

About the ToString rules you can see Fildor's commets : See Custom date and time format strings .

Sign up to request clarification or add additional context in comments.

3 Comments

Where to include this ? In Model or ... ?
You can replace : [Display(Name = "Datum i vrijeme slanja")] public DateTime DateAndTime { get; set; } = DateTime.Now; to [Display(Name = "Datum i vrijeme slanja")] public string DateAndTime { get; set; } = DateTime.Now.ToString("dd/MM/yyyy HH:mm"); About the tostring rules you can see Fildor's commets : See Custom date and time format strings .
Not sure how it is good idea to store day as String but however thank you for suggestion and you answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.