Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problem with Date formatting

Problem with Date formatting

Scheduled Pinned Locked Moved ASP.NET
debugginghelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    just4ulove7
    wrote on last edited by
    #1

    Hi, This is a code to get the starting and ending week dates. i.e week starting from Monday - Sunday. I want the dates in dd/MM/yyyy format. When I debug the code, it shows me the date in strFromdate and strTodate in dd/MM/yyyy format. But when it writes to the label in the aspx page it shows in the format MM/dd/yyyy hh:mm:ss format. Can anyone let me know where i m going wrong...??? DateTime dtNow = DateTime.Parse(DateTime.Today.ToString("MM/dd/yyyy")); int ToDayNumber = DayOfWeekNumber(dtNow.DayOfWeek); FromDate= dtNow.AddDays(1 - ToDayNumber); ToDate=dtNow.AddDays(7 - ToDayNumber); string strFromDate = FromDate.ToString("dd/MM/yyyy",new CultureInfo("en-GB")); string strToDate = ToDate.ToString("dd/MM/yyyy",new CultureInfo("en-GB")); Thanks

    M 1 Reply Last reply
    0
    • J just4ulove7

      Hi, This is a code to get the starting and ending week dates. i.e week starting from Monday - Sunday. I want the dates in dd/MM/yyyy format. When I debug the code, it shows me the date in strFromdate and strTodate in dd/MM/yyyy format. But when it writes to the label in the aspx page it shows in the format MM/dd/yyyy hh:mm:ss format. Can anyone let me know where i m going wrong...??? DateTime dtNow = DateTime.Parse(DateTime.Today.ToString("MM/dd/yyyy")); int ToDayNumber = DayOfWeekNumber(dtNow.DayOfWeek); FromDate= dtNow.AddDays(1 - ToDayNumber); ToDate=dtNow.AddDays(7 - ToDayNumber); string strFromDate = FromDate.ToString("dd/MM/yyyy",new CultureInfo("en-GB")); string strToDate = ToDate.ToString("dd/MM/yyyy",new CultureInfo("en-GB")); Thanks

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, How do you write those values to the label controls?

      J 1 Reply Last reply
      0
      • M minhpc_bk

        Hi there, How do you write those values to the label controls?

        J Offline
        J Offline
        just4ulove7
        wrote on last edited by
        #3

        this is how i write in aspx page (sorry ... its in a td, not in a label) <%=FromDate%> to <%=ToDate%> Thanks

        M 1 Reply Last reply
        0
        • J just4ulove7

          this is how i write in aspx page (sorry ... its in a td, not in a label) <%=FromDate%> to <%=ToDate%> Thanks

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          Because you use the FromDate, ToDate objects of the DateTime in the expression, so at runtime the ASP.NET will call the DateTime.ToString() method without specifying the format pattern in the method paramater. As a result you will see something that you don't expect, instead you can try with one of the two options below: + Use the objects of the string type:

          <%= strFromDate %> to <%= strToDate%>

          + Use the objects of the DateTime type:

          <%= FromDate.ToString("dd/MM/yyyy",new CultureInfo("en-GB")) %>
          to <%= ToDate.ToString("dd/MM/yyyy",new CultureInfo("en-GB"))%>

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups