Date Format

S

shapper

Hello,

I would like to display a date on the format yyyy/mm/dd:

(string)ViewData["StartDate"] ??
ViewData.Model.SlidePaper.Slide.StartDate.ToString("yyyy/mm/dd")

This is not working ... But as far as I remember was only to place the
format inside the ToString or not?

Thanks,
Miguel
 
P

Peter Morris

"Is not working"? I suspect you prefer better problem descriptions from
users of your software? :)

static void Main(string[] args)
{
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd"));
Console.ReadLine();
}

Works fine here. Make sure you use MM and not mm (which is minutes).


Pete
 
S

shapper

"Is not working"?  I suspect you prefer better problem descriptions from
users of your software? :)

  static void Main(string[] args)
  {
   Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd"));
   Console.ReadLine();
  }

Works fine here.  Make sure you use MM and not mm (which is minutes).

Pete

Sorry, you are right. I get the following error:

No overload for method 'ToString' takes '1' arguments

on:
(string)ViewData["StartDate"] ??
ViewData.Model.SlidePaper.Slide.StartDate.ToString("yyyy-MM-dd")

But on the same page I have:
DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss")

and it works fine.

Thanks,
Miguel
 
J

Jon Skeet [C# MVP]

Sorry, you are right. I get the following error:

No overload for method 'ToString' takes '1' arguments

on:
(string)ViewData["StartDate"] ??
ViewData.Model.SlidePaper.Slide.StartDate.ToString("yyyy-MM-dd")

But on the same page I have:
DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss")

and it works fine.

So what is the type of the StartDate property?
 
S

shapper

Sorry, you are right. I get the following error:
No overload for method 'ToString' takes '1' arguments
on:
(string)ViewData["StartDate"] ??
ViewData.Model.SlidePaper.Slide.StartDate.ToString("yyyy-MM-dd")
But on the same page I have:
DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss")
and it works fine.

So what is the type of the StartDate property?

--
Jon Skeet - <[email protected]>
Web site:http://www.pobox.com/~skeet 
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com

StartDate is of type DateTime ...

When I place the mouse over StartDate VS shows DateTime?
Slide.StartDate
 
J

Jon Skeet [C# MVP]

shapper said:
StartDate is of type DateTime ...

When I place the mouse over StartDate VS shows DateTime?
Slide.StartDate

So it's not of type DateTime, it's of type DateTime? i.e.
Nullable<DateTime>. Change your code to:

ViewData.Model.SlidePaper.Slide.StartDate.Value.ToString("yyyy-MM-dd")
 
B

Bjørn Brox

shapper skrev:
Didn't know that.
Just remember to test on null first because using a nullable DateTime
struct is a common way to flag a undefined/unset value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top