12675

Extracting just date from returned date and time by SQL server to show in web pages

Question:

I retrieve the date from sql server and want to display the date as "November 20, 2014" in my view / web page. The problem is SQL server returned the date and time (12/3/2014 12:00:00 AM). Below is my controller action and view

Controller

public ActionResult Index() { NewsViewModel NewsVM = new NewsViewModel(); //Initializing the view model var news = db.WASA_News.ToList();// getting all news from database NewsVM.WasaNews = news; //Adding the news into the viewmodel return View(NewsVM);// Returning ViewNodel }

Partial View

<div class="col-md-9 animated fadeInLeft"> <span>@Html.DisplayFor(modelItem => item.News_Date)</span> <span>@Html.DisplayFor(modelItem => item.News_Title)</span>

@Html.DisplayFor(modelItem => item.News_Description)

</div>

any help regarding the extracting the date on desired format.

Answer1:

You just need to format the date. <span>@Model.item.News_Date.ToString("d")</span> should format the date only.

If you can edit your model, you can alternatively add the DisplayFormat attribute:

[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)] public DateTime News_Date { get; set; }

And if you want to stick with the Html helper method, you can use the Value helper.

@Html.ValueFor(modelItem => modelItem.News_Date, "{0:MM/dd/yyyy}")

Recommend

  • how to make my program to wait for a particular thread and also not affecting my GUI's interact
  • how C++ Implicitly convert c style string to a string object?
  • Does pImpl fundamentally solve C++ DLL issue?
  • How to activate JS data-filter when page loads?
  • ObjectMaterialize in EF not firing on first level query
  • How should I start to implement RESTful web service?
  • CakePHP 3: change order in dateWidget
  • How to bind comma separated list of values to List
  • Why I can't use uniform1f instead of uniform4f for setting a vec4 uniform?
  • Stitching 2 images (OpenCV)
  • Eloquent Relationship on Partial Match
  • d3.js selection conditional rendering
  • How can I reset dropdown data if modal closed on vue component?
  • Is it better to have roles as a column on my users table, or do it through join tables (Roles &
  • How to merge keras sequential models with same input?
  • Returning the auto incrementing value after an insert using slick
  • Django model for a Postgres view
  • Activation Function choice for Neural network
  • NHibernate manually control fetching
  • How to getText() from the input field of an angularjs Application
  • Combining two different ActiveRecord collections into one
  • Angularjs pass function from Controller to Directive (or call controller function from directive) -
  • Django invalid literal for int() with base 10
  • How can I extract results of aggregate queries in slick?
  • Eloquent update method change created_at timestamp
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • MongoDB in PHP using aggregate to group by _id is null not working
  • How to extract text from Word files using C#?
  • How to check if every primary key value is being referenced as foreign key in another table
  • Sending data from AppleScript to FileMaker records
  • Turn off referential integrity in Derby? is it possible?
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?