Skip to content

Instantly share code, notes, and snippets.

@Maarten88
Last active December 28, 2017 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Maarten88/5565361 to your computer and use it in GitHub Desktop.
Save Maarten88/5565361 to your computer and use it in GitHub Desktop.
MVC4 Bootstrap EditorTemplate for DateTime, TimeSpan and combined values (using a DateAndTime class)
using System;
using System.ComponentModel.DataAnnotations;
using Auction.Web.Utility;
namespace Auction.Web.Areas.Seller.Models
{
public class DateAndTime
{
DateTime localDateTime;
// bind datetime to a database field in UTC time
public DateAndTime()
{
}
public DateAndTime(DateTime datetime)
{
//his.localDateTime = DateTime.SpecifyKind(datetime, DateTimeKind.Utc).ToLocalAuctionTime();
this.localDateTime = datetime;
}
public DateTime DateTime
{
get
{
return this.localDateTime.ToUtcTime();
}
set
{
this.localDateTime = DateTime.SpecifyKind(value, DateTimeKind.Utc).ToLocalAuctionTime();
}
}
// Bind Date and Time to input fields in the views
// [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
// [DateOnly]
public string Date
{
get
{
return localDateTime.Date.ToString("d");
}
set
{
DateTime date = DateTime.Parse(value);
localDateTime = date + localDateTime.TimeOfDay;
}
}
// [DisplayFormat(DataFormatString = "{0:hh:mm}", ApplyFormatInEditMode = true)]
public string Time
{
get
{
return localDateTime.TimeOfDay.ToString("t");
}
set
{
TimeSpan time = TimeSpan.Parse(value);
localDateTime = localDateTime.Date + time;
}
}
[DisplayFormat(DataFormatString = "{0:HH:mm}", ApplyFormatInEditMode = true)]
public DateTime DayTime
{
get
{
return Convert.ToDateTime(localDateTime.ToString());
}
set
{
localDateTime = localDateTime.Date + (value - value.Date);
}
}
}
}
@model Auction.Web.Areas.Seller.Models.DateAndTime
<div class="control-group@(Html.ValidationErrorFor(m => m, " error"))">
@Html.LabelFor(m => m, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(m => m.Date, new { @class="datepicker", data_date=Model.Date, data_date_format=System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.Replace("M", "m") })
@Html.TextBoxFor(m => m.Time, new { @class="timepicker", data_provider="timepicker" } )
@Html.ValidationMessageFor(m => m.Date, null, new { @class="help-inline" })
@Html.ValidationMessageFor(m => m.Time, null, new { @class="help-inline" })
</div>
</div>
@model DateTime?
@{
DateTime dt;
if (Model.HasValue)
{
dt = (DateTime)Model;
}
else
{
dt = DateTime.Now;
}
}
<div class="control-group@(Html.ValidationErrorFor(m => m, " error"))">
@Html.LabelFor(m => m, new { @class = "control-label" })
<div class="control input-append date datepicker" data-date="@dt.ToString("dd-MM-yyyy")">
@Html.TextBox(@dt.ToString("dd-MM-yyyy"), ViewData.TemplateInfo.FormattedModelValue)
<span class="add-on"><i class="icon-th"></i></span>
@Html.ValidationMessageFor(m => m)
</div>
</div>
@model TimeSpan?
@{
TimeSpan ts;
if (Model == null)
{
ts = TimeSpan.FromMinutes(10);
}
else
{
ts = Model.Value;
}
}
<div class="control-group@(Html.ValidationErrorFor(m => m, " error"))">
@Html.LabelFor(m => m, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(m => m, "hh\\:mm", new { @Value = ts.ToString("hh\\:mm"), @class="timepicker", data_provider="timepicker" } )
@Html.ValidationMessageFor(m => m, null, new { @class="help-inline" })
</div>
</div>
@udayashree
Copy link

Really useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment