How to get full date with full month name like 02

2019-07-18 08:48发布

问题:

I want to load date to the text box with full month, this means that the users will get the current date and time every time they want to save the date on the text box. They won't write anything

Here is the format i want "02 November 2015" but am not getting what am looking for. I got this results "DD-NOV-2015" and that's not what am looking for.

Should you know any simple way i can do it using j query,ajax or any,please help

Please see my code below

Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        this.txtInceptiondate.Text = DateTime.Now.ToString("DD-MMM-yyyy",System.Globalization.CultureInfo.InvariantCulture).ToUpper();
    }

Saving to the database

    protected void tbnSave_Click(object sender, EventArgs e)
    {
        IList<tblPolicy> _Inception = _dc.tblPolicies.Where(a => a.Name == txtname.Text.ToString()).ToList();

        if (_Inception != null)
        {
            if (_Inception.Count() == 0)
            {
                tblPolicy _Policy = new tblPolicy
                {
                    Name = txtname.Text,
                    PolicyName = txtpolicyname.Text,
                    InceptionDate = txtInceptiondate.Text
                };
                _dc.tblPolicies.InsertOnSubmit(_Policy);
                _dc.SubmitChanges();
                lblresults.Visible = true;
                lblresults.Text = "Welcome " + txtname.Text + " ! , Your new policy " + txtpolicyname.Text + " is well recived!";

                txtpolicyname.Text = "";
                txtname.Text = "";
                txtInceptiondate.Text = "";
            }
        }

    }

回答1:

There is no DD as a custom date and time specifier. These specifiers are case sensitive. That's why it reflects itself to the result as it is.

You need to use dd MMMM yyyy format instead. And don't use ToUpper method since you want November not NOVEMBER.

this.txtInceptiondate.Text = DateTime.Now.ToString("dd MMMM yyyy", CultureInfo.InvariantCulture);

Further reading:

  • Custom Date and Time Format Strings


回答2:

You can try to use MMMM to get the full name of the month. Check the MSDN. Try this:

this.txtInceptiondate.Text = DateTime.Now.ToString("dd MMMM yyyy",System.Globalization.CultureInfo.InvariantCulture).ToUpper();


回答3:

Try this out

 DateTime now = DateTime.Now.Date;
 this.txtInceptiondate.Text = now.ToString("D"); 

It will give result in this format 02 November 2015