module.exports = dateHelper = 
	setFormat: (dateFormat = 'd/m/Y H:i') ->
		dateHelper.format.bind dateHelper, dateFormat

	parseFrom: (format, date) ->
		parsed = {}
		matches = date.match(///#{format.replace /\w+/g, '(\\w+|\\d+)'}///)[1..]
		for fm, i in format.match /\w+/g
			parsed[fm] = matches[i]

		year = parsed.Y or parsed.y or 0
		month = (parsed.F or parsed.m or parsed.M or parsed.n or 0)-1
		day = parsed.d or parsed.D or parsed.j or 0
		hours = parsed.g or parsed.G or parsed.h or parsed.H or 0
		minutes = parsed.i or 0
		seconds = parsed.s or 0

		(new Date year, month, day, hours, minutes, seconds).getTime()

	changeFormat: (fromFormat, toFormat, date) ->
		time = dateHelper.parseFrom fromFormat, date
		dateHelper.format toFormat, time

	format: (dateFormat = 'd/m/Y H:i', milliseconds) ->
		if typeof milliseconds is 'string' and (new Date(milli = Date.parse milliseconds)) isnt 'Invalid Date'
			milliseconds = milli

		if typeof milliseconds is 'object' and milliseconds.date? and milliseconds.time?
			milliseconds = Date.parse milliseconds.date.split('-').reverse().join('/')+' '+milliseconds.time

		$.datepickr::formatDate dateFormat, milliseconds