Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the 0 in "{0:MM dd yyyy}" do?

I'm new to the C#/MVC world. I spend a lot of time today figuring out how to display a DateTimeOffset object in the format i want. Finally got it working this way.

Html.TextBoxFor(model => model.DeliveryDate,"{0:MM/dd/yyyy}", 
                     new { htmlAttributes = new { @class = "datepicker" } })  

But I still don't understand the importance of '0' in the format string. the page breaks if i replace the 0 with any other number or totally remove it. Can someone help me understand this?

like image 766
Ketki Avatar asked Mar 31 '16 19:03

Ketki


People also ask

What is T and Z in DateTime C?

A UTC DateTime is being converted to text in a format that is only correct for local times. This can happen when calling DateTime. ToString using the 'z' format specifier, which will include a local time zone offset in the output.

What is DateTime string format?

A date and time format string defines the text representation of a DateTime or DateTimeOffset value that results from a formatting operation. It can also define the representation of a date and time value that is required in a parsing operation in order to successfully convert the string to a date and time.

How to display date in dd mm yyyy format in Mvc?

var userDt = DateTime. Now. ToString("MM/dd/yyyy hh:mm tt"); // return 08/05/2016 12:56 PM.


4 Answers

From String.Format Method

The {0} in the format string is a format item. 0 is the index of the object whose string value will be inserted at that position. (Indexes start at 0.) If the object to be inserted is not a string, its ToString method is called to convert it to one before inserting it in the result string.

like image 105
Eric Avatar answered Oct 25 '22 01:10

Eric


When you use the string.Format you can pass the space for arguments like {0}, {1}, etc which is the indexes you pass as arguments for the method. It is the same for asp.net razor helpers.

You also can provide the format after the index separating by :, for sample: {0:0.00} as format for a number with 2 decimals places or {1:dd/MM/yyyy} for dates etc.

String Interpolation

There is a new way to implement it using the String Interpolation. Basically, you can concat the values on your string without generating new strings. For sample:

var i = 18;
var s = $"You are {age} years old.";

Since you start the string with $, you can pass arguments between { and }. You also can use the same formats to format your data as you use on string.Format. For sample:

var today = $"Today is {DateTime.Now:D}";

var date = DateTime.Now.Add(1);
var tommorrow = $"Tommorrow is {date:dd/MM/yyyy}";
like image 21
Felipe Oriani Avatar answered Oct 24 '22 23:10

Felipe Oriani


That's a format string with parameters (like used in e.g. Console.WriteLine, or string.Format). The {0} would be the placeholder for the first argument, and {0:mm/dd/yyyy} is simply a format string to convert the first argument to a string.

like image 40
Joey Avatar answered Oct 24 '22 23:10

Joey


See the documentation for String.Format():

https://msdn.microsoft.com/en-us/library/system.string.format.aspx

In a nutshell, when the model is rendered to HTML text, the DeliveryDate object value will be passed to String.Format(), where {0} indicates the index of the first value in an array of values being passed to Format(). So {0:MM/dd/yyyy} just means to format the first value in the array using date components. Basically, it will do something like this internally:

String s = SomeValueArray[0].ToString("MM/dd/yyyy");
like image 33
Remy Lebeau Avatar answered Oct 25 '22 00:10

Remy Lebeau