5

Problem that needs to be solved:

  • Getting time and date from a MVC application

My specific problem: I have a parking management application. As it is possible to book a parking spot up-front I need to know parking start time and date. For my model I used DateTime because MSDN advertises it as full featured time and date data type:

The .NET Framework DateTime class provides a full-featured interface for writing programs that deal with time.

My research: I have searched answers or typical solutions(best practises) for this kind of problem and almost all I got was:

  • problems with globalization
  • problems with timezones
  • different date formats

But nothing considering how to get date and time conveniently with MVC application.

Closest answer I found was Scott Hanselman's 2009 solution - http://www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders.aspx

Other solutions that people tend to use are different jQuery plugins.

To me these solutions seem to be sligthly over-engineered. As I dont know where else to ask and if these are the only solutions or not - my question is:

  • What are my options getting time and date with MVC - best practice?

In my model I use:

[DataType(DataType.DateTime), DisplayFormat(DataFormatString = "{0:HH:mm dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime? ParkingStartTime { get; set; }

In my view I use:

 <div class="editor-label">
        @Html.LabelFor(vm => vm.newCarOnParkingSpot.ParkingStartTime)
    </div>
    <div class="editor-field">
        @Html.EditorFor(vm => vm.newCarOnParkingSpot.ParkingStartTime)
        @Html.ValidationMessageFor(vm => vm.newCarOnParkingSpot.ParkingStartTime)
    </div>

This solution give me a HTML5 datepicker, but no time inserting option : https://i.stack.imgur.com/fGPTV.png


It is my first question, so please be gentle if I have mistaken with this question.

Sleven
  • 55
  • 1
  • 8
  • 2
    The problem you have defined is not very clear `Getting time and date from a MVC application`. What do you mean? If you're just looking for a UI component, anything will do, even a raw text field is fine, MVC will bind it appropriately if it is in the correct format. – NibblyPig Jul 21 '15 at 14:50
  • if the problem is to display and send date time on the client side, there are many optins available like bootstrap or jquery ui. The support for HTML5 datetime is removed from the browsers ; http://stackoverflow.com/questions/21263515/why-is-html5-input-type-datetime-removed-from-browsers-already-supporting-it – man_luck Jul 21 '15 at 14:58
  • 1
    @SLC & man_luck - I'm sorry for not specifing my question. I would like to know different options for getting time and date on the UI side. Yes, I have seen some jQuery plugins, but I was wondering what are the best practises or most commonly used solutions. Also saving that date and time in DateTime data type to db should not get too complicated(as I am a trainee/junior level developer). – Sleven Jul 21 '15 at 15:29
  • 1
    There's no best practice for this, the jquery date picker would work fine and is pretty simple. – NibblyPig Jul 21 '15 at 15:35
  • This counts as an answer for my question. I dont have enough reputation to upvote your comment, but I will add my jQuery DateTimePicker solution here soon. Thank you for clarifing this subject. – Sleven Jul 21 '15 at 15:42
  • it seems your problem is in the CultureUiInfo and CultureInfo which you can control them in your Global.asax in Application_BeginRequest, if you still confused i can write it down in the answer – user5135401 Jul 21 '15 at 16:39
  • @user5135401 No-no, no need. It was not about the CultureInfo. It was more a global question about how should one operate with date and time in MVC. But thanks for responding. – Sleven Jul 21 '15 at 16:44
  • 1
    Real quick... When your adorn your property with the attribute and value, [DataType(DataType.Date)], you are stating to MVC that you only want the date portion (no time). – ventaur Jul 21 '15 at 18:41
  • @ventaur yup. Fixed that. Thanks! – Sleven Jul 22 '15 at 06:48

1 Answers1

0

So as @SLC suggested I used jQuery. I chose jQuery DateTimePicker http://xdsoft.net/jqplugins/datetimepicker/

According to SLC:

There's no best practice for this, the jquery date picker would work fine and is pretty simple

And he is right. I fixed my problem within 7 minutes.

All I did was:

  • added jQuery DateTimePicker files to my project(from the site above)
  • followed the instructions provided on that site(added 2 script and 1 css file to the cshtml file, added an id to the input field needed and called jQuery function on previously added id - Voila!)
  • 0 minutes spent on getting that data saving to db with DateTime data type

I feel that one place for improvment is my jQuery function calling. Is there a better way than this?:

<script type="text/javascript">
$(document).ready(function () {
    jQuery('#datetimepicker').datetimepicker();
});

Sleven
  • 55
  • 1
  • 8
  • Take a look at how MVC renders the editor for date/times. If there is no CSS class, look at editor templates so you can render the inputs with a consistent class. Then, you can put your jQuery hookup code in your main shared template (for all pages), but use your new class instead of an id. Have it in one place and work for all your date/time inputs. jQuery('.date').datetimepicker(); – ventaur Jul 22 '15 at 13:47