Click here to Skip to main content
15,912,504 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
I need to have the time in 3:15 PM format without seconds. I am using jquery timepicker but its always giving me the error. 
THE VALUE '3:15 PM' IS NOT VALID FOR TIME
The code is provided... Can anyone please help....


Model Class

    [Required(ErrorMessage = "{0} is required")]
    [Display(Name = "Test Time")]
    [DataType(DataType.Time)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh:mm}")]
    //[RegularExpression(@"^(0[1-9]|1[0-2]):[0-5][0-9] (am|pm|AM|PM)$", ErrorMessage = "Invalid Time.")]
    public TimeSpan TestTime { get; set; }

<pre>

View

&lt;div class="col-md-6"&gt;
    @Html.LabelFor(model =&gt; model.TestTime, new { @class = "control-label" })
    &lt;div class="input-group"&gt;
        &lt;div class="input-group "&gt;
            &lt;span class="input-group-addon" style="background:white"&gt;
                &lt;i class="fa fa-clock-o" style="color:#179CDC;"&gt;&lt;/i&gt;
            &lt;/span&gt;
            @*&lt;input type="text" class="form-control timepicker timepicker-no-seconds"&gt;*@
            @Html.TextBoxFor(model =&gt; model.TestTime, "", new { @class = "form-control timepicker timepicker-no-seconds time" })
            @*@Html.TextBoxFor(model =&gt; model.TestTime, "", new { @class = "form-control timepicker timepicker-24 time" })*@
            &lt;span class="input-group-btn"&gt;
                &lt;button class="btn btn-info" type="button"&gt;&lt;i class="fa fa-clock-o"&gt;&lt;/i&gt;&lt;/button&gt;
            &lt;/span&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    @Html.ValidationMessageFor(model =&gt; model.TestTime, string.Empty, new { @class = "small text-uppercase text-danger" })
&lt;/div&gt;


What I have tried:

Script....

<script>
$(document).ready(function () {
$('#testtime-edit-details .time').timepicker({
//'showDuration': true,
////'timeFormat': 'h:i A',
//autoclose: true
//timeFormat:'h:mm p',
// ampm: true,

timeFormat: 'HH:mm:ss',
minTime: '11:45:00',
maxHour: 20,
maxMinutes: 30,
startTime: new Date(0,0,0,15,0,0),
interval: 15,
autoclose: true
});
});
Posted
Updated 18-Mar-16 1:24am
v2
Comments
Richard Deeming 18-Mar-16 9:05am    
Try changing the type of the property in your model to DateTime.
Member 10740412 18-Mar-16 9:48am    
i already tried that but it's giving me this error...
Cannot implicitly convert type 'System.DateTime' to 'System.TimeSpan'.
Since the datatype for test date in the database is time(7).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900