Click here to Skip to main content
15,883,847 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi,

I have been asked to return the string representation of the given DateTime in the format: Monday, July 30, 2013 14:18:00 (AM/PM). Please see my code below. I am not far off the right answer, but I must have gone wrong somewhere in my syntax.

Corrections and any accompanying explanations would be much appreciated.

Kind regards

What I have tried:

 {
        /****************************************************************
        
        Return the string representation of the given DateTime in the format: Monday, July 30, 2013 14:18:00 (AM/PM) .
        
        *****************************************************************/
        public static string GetFullDisplayDateTime(DateTime datetime)
        {

            return datetime.ToString("dddd","MMMM" "dd", "yyyy", "zzz" );


        }
    }
}
Posted
Updated 11-Jul-17 1:21am
Comments
F-ES Sitecore 11-Jul-17 6:48am    
Member 13302374 11-Jul-17 6:59am    
Thanks for the link. Please bear in mind I'm new and learning, so not everything will be obvious.

return datetime.ToString("dddd, MMMM dd, yyyy HH:mm:ss");

That's my updated code, which still brings an error. It must be the last part, but I've followed the documentation as best as possible
jimmson 11-Jul-17 7:13am    
You need to espace ':' in this string. Like this: @"HH\:mm"
Member 13302374 11-Jul-17 7:16am    
return datetime.ToString("dddd, MMMM dd, yyyy HH\:mm\:ss");

Like this?

Also, I need to have AM or PM automatically written on the end. I saw it on the website that was provided above, but can't see the code for it

1 solution

Why don't you use a standard format?
e.g.
var ci = new CultureInfo("en-US");
var dateTimeString = now.ToString("F", ci);

Don't forget to use the right CultureInfo.

Another one:
var dateTimeString = now.ToString("dddd, MMMM dd yyyy HH:mm:ss tt", ci);
 
Share this answer
 
v2
Comments
Member 13302374 11-Jul-17 7:29am    
Thanks. I will try this. As far as my code

return datetime.ToString("dddd, MMMM dd, yyyy HH:mm:ss");

do you know what would be the correction that I am missing? I need it to say automatically whether it's AM or PM after the time. When I run this code, the error box tells me the time is coming out as 00:00:00, so there's definitely something wrong somewhere
TheRealSteveJudge 11-Jul-17 7:32am    
Just try it. It will automatically show AM or PM.
The standard formats are described with examples
in F-ES Sitecore's comment https://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx
Member 13302374 11-Jul-17 8:48am    
your code didn't work, I guess because I'm on a specific training program and they only want the expected answers. I will try your code when I get on my own computer. As for my code, what is the small correction that I am missing out?
TheRealSteveJudge 11-Jul-17 9:06am    
I updated the solution with another approach.
Both my suggestions are tested and work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900