locked
Problem in handling writing date in full. RRS feed

  • Question

  • User662762443 posted

    Aprenda a pronunciar I have the following problem converting a date I take from the bank and reading it in full. In my database the date field is like 201910, so I will need to write it in full, for example: October 2019, by default all dates in the database default to 2019 = YYYY which is the year and followed by the 10 which refers to the month, soon I will need to write in html in October 2019, but what I tried to do does not work.

    protected void Page_Load(object sender, EventArgs e)
    
            {
    
                if (!IsPostBack)
    
                {
    
     
    
                    //Fill in the Header Information
                    //I'm passing the date and output in the header,  
                    //because I get the temporary list information using the Session.
                    
                    strConstrution.Append(string.Format(header,"", date, output));
    
                    int count = 0;
    
                    foreach (Financing data in financing.Where(o => o.BaseMonthYear==date))
    
                    {
    
                        string monthextensive = System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(Convert.ToString(data.
    YearMonthRevenues)).ToLower();
    
     
    
                        strConstrution.Append(string.Format(dataFinancing, data.Company.CompanyName, data.Company.StateRegistration, data.Company.Cnpj, data.Company.ShortName));
    
                        strConstrution.Append(string.Format(dataValues, data.YearMonthRevenues.ToString(), data.ValueICMSExpected, data.TotalRecollection, dados.VlrFinanciamento, dados.VlrTotalLiberacao));
    
     
                    Relatorio.InnerHtml += strConstrution.ToString();
    
     
    
                }
         }

    In my code I implemented this, but it is not working. As implemented the code presents the error stating that I cannot convert integer to string.

    string monthextensive = System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(Convert.ToString(data.
    YearMonthRevenues)).ToLower();

    In my bank the date 201910 is like type int.

    Friday, October 25, 2019 2:34 AM

Answers

  • User-719153870 posted

    Hi rtaVix,

    The whole description about your requirement is a little confusing.

    I have the following problem converting a date I take from the bank and reading it in full.

    When you say "bank", you mean a table whose name is "bank" in your database, right?

    In my database the date field is like 201910, so I will need to write it in full, for example: October 2019, by default all dates in the database default to 2019 = YYYY which is the year and followed by the 10 which refers to the month, soon I will need to write in html in October 2019,

    From this sentence, you mean you want to read the date value in your database which is like "201911" and write it to your html page in the format like "October 2019"?

    And you said, "In my bank the date 201910 is like type int.", the date filed in your database is int type?

    If so, the whole problem can be very simple, you want to show an int value "201911" as a datetime value "October 2019" in your page.

    This can be easily done by the datetime.tostring("Y") method, use a string.substring() method to split your date value first.

    Please refer to below demo:

    protected void Page_Load(object sender, EventArgs e)
            {
                int intdate = 201910;//let's assume this is the int type date value in your database which you want to fetch
                string strdate = intdate.ToString();//then, this should be the date value you fetch in your web program and it should be string, its format is like "YYYYMM"
                int year = Convert.ToInt32(strdate.Substring(0,4));//get the year of your date in int type
                int month = Convert.ToInt32(strdate.Substring(4, 2));//get the month of your date in int type
                DateTime date = new DateTime(year, month, 1);//here, you will convert this string type date value to a datetime type value in c#
                Response.Write(date.ToString("Y"));//let's read it in html in the expected format
            }

    Below is the result:

    If i misunderstood anything, please feel free to let me know.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 25, 2019 5:48 AM

All replies

  • User-719153870 posted

    Hi rtaVix,

    The whole description about your requirement is a little confusing.

    I have the following problem converting a date I take from the bank and reading it in full.

    When you say "bank", you mean a table whose name is "bank" in your database, right?

    In my database the date field is like 201910, so I will need to write it in full, for example: October 2019, by default all dates in the database default to 2019 = YYYY which is the year and followed by the 10 which refers to the month, soon I will need to write in html in October 2019,

    From this sentence, you mean you want to read the date value in your database which is like "201911" and write it to your html page in the format like "October 2019"?

    And you said, "In my bank the date 201910 is like type int.", the date filed in your database is int type?

    If so, the whole problem can be very simple, you want to show an int value "201911" as a datetime value "October 2019" in your page.

    This can be easily done by the datetime.tostring("Y") method, use a string.substring() method to split your date value first.

    Please refer to below demo:

    protected void Page_Load(object sender, EventArgs e)
            {
                int intdate = 201910;//let's assume this is the int type date value in your database which you want to fetch
                string strdate = intdate.ToString();//then, this should be the date value you fetch in your web program and it should be string, its format is like "YYYYMM"
                int year = Convert.ToInt32(strdate.Substring(0,4));//get the year of your date in int type
                int month = Convert.ToInt32(strdate.Substring(4, 2));//get the month of your date in int type
                DateTime date = new DateTime(year, month, 1);//here, you will convert this string type date value to a datetime type value in c#
                Response.Write(date.ToString("Y"));//let's read it in html in the expected format
            }

    Below is the result:

    If i misunderstood anything, please feel free to let me know.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 25, 2019 5:48 AM
  • User379720387 posted

    yourdatetime.ToString("MMMM yyyy")

    https://www.mikesdotnetting.com/article/23/date-formatting-in-c

    Sunday, October 27, 2019 2:16 PM