HOTSPOT
You are developing an application in C#.
The application will display the temperature and the time at which the temperature was
recorded. You have the following method (line numbers are included for reference only):
You need to ensure that the message displayed in the lblMessage object shows the time
formatted according to the following requirements:
The time must be formatted as hour:minute AM/PM, for example 2:00 PM.
The date must be formatted as month/day/year, for example 04/21/2013.
The temperature must be formatted to have two decimal places, for example 23-45.
Which code should you insert at line 04? (To answer, select the appropriate options in the answer area.)
date format, it wrong it should be {0:mm/dd/yy}
No, but it’s difficult to see! Take a look at the wildcards:
‘MM’: would give the month.
‘mm’: gives MINUTES!!
correct answer : String.Format(“Temperature at {0:hh:mm} {0:d} temp {1:N2}”, DateTime.Now, 25);
O:t -represents the first character of AM/PM
0:mm/dd/yy – mm are minutes, MM are months
https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings#the-t-custom-format-specifier
I worked out the correct code snippets:
string output = String.Format(“Temperature at {0:t} {0:d} temp {1:N2}”, DateTime.Now, 25);
The answer is
string output = String.Format(“Temperature at {0:t} {0:d} temp {1:N2}”, DateTime.Now, 25);
{0:d} refer to ShortDate
You are right
The answer is:
//modify the vlaue of en-us to Culture.
Thread.CurrentThread.CurrentCulture = new CultureInfo(“en-us”);
string msg = string.Format(“Temperature at {0:t} on {0:d} : {1:N2}”, DateTime.Now, 20);
Correct
Thread.CurrentThread.CurrentCulture = new CultureInfo(“en-us”);
string msg = string.Format(“Temperature at {0:t} on {0:d} : {1:N2}”, DateTime.Now, 20);
correct!
{0:t}
{0:d}
{1:N2}
if the year is 2013, then it must be “yyyy”. But there is no such Option.
{0:t}
{0:d}
{1:N2}
confirmed in Visual Studio:
string.Format(“time={0:t} date={0:d} temp={1:N2}”, DateTime.Now, 2)
——————————————
time=01:24 AM date=05/10/2016 temp=2.00
{0:d} and {0:t} is culture specific. For me this code print
time=23:43 date=14.02.2017
If I add before
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
then work correctly.
{0:t}
{0:d}
{1:N2}
string output = String.Format(“Temperature at{0:t} {0:d} temp {1:N2}”, DateTime.Now, 45);
{0:t}
{0:d}
{1:N2}