Date Format

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    Date Format

    Hello,

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

    (string)ViewDat a["StartDate"] ??
    ViewData.Model. SlidePaper.Slid e.StartDate.ToS tring("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
  • Peter Morris

    #2
    Re: Date Format

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

    static void Main(string[] args)
    {
    Console.WriteLi ne(DateTime.Now .ToString("yyyy-MM-dd"));
    Console.ReadLin e();
    }

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


    Pete

    Comment

    • shapper

      #3
      Re: Date Format

      On Sep 18, 12:57 pm, "Peter Morris" <mrpmorri...@SP AMgmail.comwrot e:
      "Is not working"?  I suspect you prefer better problem descriptions from
      users of your software? :-)
      >
        static void Main(string[] args)
        {
         Console.WriteLi ne(DateTime.Now .ToString("yyyy-MM-dd"));
         Console.ReadLin e();
        }
      >
      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)ViewDat a["StartDate"] ??
      ViewData.Model. SlidePaper.Slid e.StartDate.ToS tring("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

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Date Format

        shapper <mdmoura@gmail. comwrote:

        <snip>
        Sorry, you are right. I get the following error:
        >
        No overload for method 'ToString' takes '1' arguments
        >
        on:
        (string)ViewDat a["StartDate"] ??
        ViewData.Model. SlidePaper.Slid e.StartDate.ToS tring("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 - <skeet@pobox.co m>
        Web site: http://www.pobox.com/~skeet
        Blog: http://www.msmvps.com/jon.skeet
        C# in Depth: http://csharpindepth.com

        Comment

        • shapper

          #5
          Re: Date Format

          On Sep 18, 2:32 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
          shapper <mdmo...@gmail. comwrote:
          >
          <snip>
          >
          Sorry, you are right. I get the following error:
          >
          No overload for method 'ToString' takes '1' arguments
          >
          on:
          (string)ViewDat a["StartDate"] ??
          ViewData.Model. SlidePaper.Slid e.StartDate.ToS tring("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 - <sk...@pobox.co m>
          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

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Date Format

            shapper <mdmoura@gmail. comwrote:
            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<DateTi me>. Change your code to:

            ViewData.Model. SlidePaper.Slid e.StartDate.Val ue.ToString("yy yy-MM-dd")


            --
            Jon Skeet - <skeet@pobox.co m>
            Web site: http://www.pobox.com/~skeet
            Blog: http://www.msmvps.com/jon.skeet
            C# in Depth: http://csharpindepth.com

            Comment

            • shapper

              #7
              Re: Date Format

              On Sep 18, 5:19 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
              shapper <mdmo...@gmail. comwrote:
              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<DateTi me>. Change your code to:
              >
              ViewData.Model. SlidePaper.Slid e.StartDate.Val ue.ToString("yy yy-MM-dd")
              >
              --
              Jon Skeet - <sk...@pobox.co m>
              Web site:http://www.pobox.com/~skeet 
              Blog:http://www.msmvps.com/jon.skeet
              C# in Depth:http://csharpindepth.com
              Didn't know that.

              Thanks,
              Miguel

              Comment

              • =?ISO-8859-1?Q?Bj=F8rn_Brox?=

                #8
                Re: Date Format

                shapper skrev:
                On Sep 18, 5:19 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
                >shapper <mdmo...@gmail. comwrote:
                >>StartDate is of type DateTime ...
                >>When I place the mouse over StartDate VS shows DateTime?
                >>Slide.StartDa te
                >So it's not of type DateTime, it's of type DateTime? i.e.
                >Nullable<DateT ime>. Change your code to:
                >>
                >ViewData.Model .SlidePaper.Sli de.StartDate.Va lue.ToString("y yyy-MM-dd")
                >>
                >--
                >Jon Skeet - <sk...@pobox.co m>
                >Web site:http://www.pobox.com/~skeet
                >Blog:http://www.msmvps.com/jon.skeet
                >C# in Depth:http://csharpindepth.com
                >
                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.

                --
                Bjørn Brox

                Comment

                Working...