Skip to content
This repository has been archived by the owner on Jul 8, 2020. It is now read-only.

Date (The M Project 1.x)

hano edited this page Dec 3, 2013 · 1 revision

Introduction

Dates are a main topic within almost any application. No matter if you need real dates as in any kind of planning / managing / calendar application or to just count the seconds between two points in time, as you might do it in some sort of gaming applications. Anyway, a convenient date handling is a essential topic for any framework thus also for The M-Project.

Isn't there a date object in JavaScript already?

Sure, there is the native date object in JavaScript. But depending on what you are planning to do this might cause some trouble or at least some painful programming. So we decided to build an enriched wrapper around this, called M.Date. If you'd like to stay with the native date object, feel free. But you'll miss out on a lot of nice features that will ease date handling within your application.

How to create a date?

As mentioned above, M.Date is basically a wrapper to JavaScript's native date object. So one first thing you can do is to simply create a date by calling M.Date's now() method. To create a date object with a specific date, there is the create() method, that processes a given dateString:

var dateString = '24/12/2010';
var date = M.Date.create(dateString);

The create() method accepts a date string matching one of the following formats:

  • 11/30/2010

  • 30.11.2010

  • 11/30/2010 15:28:15

  • 30.11.2010 15:28:15

  • 11/30/2010 13:28:15 GMT

  • 30.11.2010 13:28:15 GMT

  • 11/30/2010 15:28:15 GMT+0200

  • 30.11.2010 15:28:15 GMT+0200

  • 30 November 2010

  • 30 November 2010 15:28:15

  • 30 November 2010 13:28:15 GMT

  • 30 November 2010 15:28:15 GMT+0200

  • 30 Nov 2010

  • 30 Nov 2010 15:28:15

  • 30 Nov 2010 13:28:15 GMT

  • 30 Nov 2010 15:28:15 GMT+0200

Is there another way to get a date?

Besides creating a date object with the current date or based on a given date string there might be use cases where you'll need to compute a date in the future or pased based on a given date. For example you might need a date object, two hours in the future, seven days in the future or one year in the past. M.Date provides a lot of methods that help you out with that. You can add or substract milleseconds, seconds, minutes, hours or days from the current date or a specific date. Let's take a look at some examples:

var date = M.Date.now();
// 12/06/2010 16:33:32

date.daysFromDate(2);
// 12/08/2010 16:33:32

date.hoursFromDate(-10);
// 12/06/2010 06:33:32

date.secondsFromDate(12345);
// 12/06/2010 19:59:17

M.Date.secondsFromNow(12345);
// 12/06/2010 20:01:55

How to get the time between two dates?

Since getting the time between two dates is a recurring issue, we implemented the timeBetween method, that covers this problem. Let's say you want to know how many days New Year is out. Such a calculation is pretty convenient with M.Date:

var daysUntilNewYear = M.Date.now().timeBetween(M.Date.create('01.01.2011'), M.DAYS);

At first we create an M.Date object using its now() method. Then we call the timeBetween method on this date and pass along our “target date” created using the create method and the desired return type, e.g. M.DAYS. That's it!