2

I am making a small app and I need to show the DateTime, but I don't know how.

I have a ViewModel:

public class ViewModel
{
    public DateTime Time{ get; set; }
}

I have a controller called TimeController:

public ActionResult Index()
{
    ViewModel v = new ViewModel();
    v.Time = DateTime.Now;
    return View(v);
}

And a View from Index:

    ViewBag.Title = "Index";
}
<h2>Hello</h2>

Can somebody explain me how I can show the time on my view?

1
  • 1
    BTW you do understand that it will be current server time? Commented Jan 24, 2013 at 9:10

3 Answers 3

5

Can somebody explain me how i can show the time in my view?

You can directly show it using @:

<div>@DateTime.Now.ToString()</div>

If your only purpose is to show DateTime, I wouldn't bother using ViewModel or atleast wouldn't store DateTime.Now just to be able to display it in View.

Sign up to request clarification or add additional context in comments.

1 Comment

Will this show the local time, regardless of a user's timezone?
3

First line of your view

@model ViewModel

This means that your view is typed with type ViewModel

Then

@Html.DisplayFor(m => m.Time)

Comments

1

Add first in Index View on the top this:@model Yourapp.Models.ViewModel Then you can do this:

Time is : @Model.Time

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.