Date conversion Error in mvc

Posted by Klbaiju under ASP.NET MVC on 6/13/2018 | Points: 10 | Views : 426 | Status : [Member] | Replies : 2
Hi,

I want to convert date in 'yyyy-mm-dd' format

following is my code

string currentYear = DateTime.Now.Year.ToString();
string Cmonth = DateTime.Now.ToString("MMMMMMMMM");
string smonth = DateTime.Now.Month.ToString();
string kk = currentYear + "-" + smonth + "-" + "01";

DateTime s = Convert.ToDateTime(kk);

when I execute above code Iam getting s as

6/1/2018 12:00:00 AM

my requirement is s should be

2018-06-01

how it is possible

Regards

Baiju




Responses

Posted by: Sheonarayan on: 6/15/2018 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Write

s.ToString("yyyy-MM-dd");


It should work.

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-Innovator on: 6/25/2018 [Member] Bronze | Points: 25

Up
0
Down
You can try this
Model Class
public class GetCurrentDateModel
{
public DateTime? CurentDate { get; set; }
}


Controller Class
public ActionResult Index()
{
GetCurrentDateModel getCurrentDate = new GetCurrentDateModel
{
CurentDate = DateTime.Now.Date
};
ViewBag.GetDate = getCurrentDate;
return View();
}


Index.cshtml view
@using MVC_Sample_App.Models
<h3>Curernt Date</h3>

@{
var getDate = ViewBag.GetDate;
}
<h4>@getDate.CurentDate.ToShortDateString()</h4>


Join Hands Change lives
Thanks & Regards
Straight Edge Society

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response