M# Tutorials

Learn to build .NET applications with M#. Our step-by-step tutorials will get you up-to-speed rapidly.
If you already know ASP.NET and C#, you can master M# in a week or two.

DateTime Property

M# DateTime is a high level representation of a date.
By adding a DateTime property, M# will generates a standard C# DateTime property with its XML documentation:

The creation of this property will also create the

datetime

SQL type:

M# gives you flexibility by defining some specific DateTime attributes allowing you to change the behaviour in your C# logic or the generated SQL code.

Creation

When typing the DateTime property, M# displays a list of different types:

  • Date: Nullable date without Time
  • Date*: Date without Time
  • Date & time: Nullable Date and Time
  • Date & time*: Date and Time

Attributes

Has date

Has date will have no effect on the database column definition or the generated C# class. M# will store this as a datetime in the database
Instead of creating two integer properties for Hour and Minute, use the DateTime property and set this attribute to false. By doing this you will get Time validation without writing a line of code.
For example the time 06:30 will be stored as "1900-01-01 06:30:00.000", "1900-01-01" corresponding to the minimum date value for SQL Server.

Has time

Has time will have no effect on the database column definition or the generated C# class. M# will store this as a datetime in the database
This will create a Date record with the date set to 0.
For example the date 01/01/2000 will be stored as "2000-01-01 00:00:00.000".

Lower bound

Lower bound will have no effect on the database column definition, but will create a new validation rule based on the value.
For example if you create the properties "DateAdded" and "DateUpdated" you can set the lower bound of DateUpdated to DateAdded:

Upper bound

Upper bound will have no effect on the database column definition, but will create a new validation rule based on the value.
It works exactly like Lower bound except that the value must be before the attribute value.

Use utc datetime format

Use utc datetime format will have no effect on the database column definition, but the model population will change the date.

LocalTime

Testing the behaviour of an application at different dates in not easy, but we often have to do this.
With the M# LocalTime class it becomes easy to manipulate and control dates in a test environment.
LocalTime is not a type like DateTime is, but more like a new class surrounding DateTime, returning DateTime result and providing you some new features.

How to use it?

Forget using the DateTime class for getting the Date/Time, replace it by LocalTime everywhere in your code to benefit from the power of LocalTime.
You can for example change the datetime of your application by using

LocalTime.Set()

. This method implements IDisposable so it is very easy to change the datetime only on a piece of code as shown on the following screenshot:

You may also be interested by

LocalTime.RedifineNow()

to set the current time of the application.

Another useful feature is

LocalTime.Stop()

. By using this you can stop the datetime:

Advantages

There are lots of advantages, but most of them are on test environment:

  • Returns a
    DateTime
    type, compatible with all DateTime extension methods.
  • Test server: Check the behaviour of the application over time, for example the generation of monthly invoices.
  • Unit test: Run all your tests at the same time, or change the date/time to test a different scenario.