var JadeDateTime = {
	Now: function() {
		return new Date();
	},
	Today: function() {
		return CoderDate.Clear(CoderDate.Now());
	},
	Clear: function(value) {
		value.setHours(0);
		value.setMinutes(0);
		value.setSeconds(0);
		value.setMilliseconds(0);
		return value;
	},
	Firstday: function(month,year)
	{
		return new Date(year,(month-1),1);
	},
	Monthdays: function(month,year)
	{
		return 32-(new Date(year,(month-1), 32).getDate());
	},
	Weekday: function(value)
	{
		var day = value.getDay();
		if(day==0){ day=7; }
		return day;
	},
	UEWeekDay: function(value){
		return Jade.iif(value==0,6,value-1);
	},
	showStoreDate: function(value)
	{
		return value.substring(6,8) + '/' + value.substring(4,6) + '/' + value.substring(0,4);
	},
	fromStoreDate: function(value)
	{
		var year=value.substring(0,4);
		var month=value.substring(4,6);
		var day=value.substring(6,8);
		return new Date(year,month-1,day);
	},
	toStoreDate: function(value)
	{
		var year=value.getFullYear();
		var month = Jade.Strings.PadLeft(value.getMonth()+1,'0',2);
		var day=Jade.Strings.PadLeft(value.getDate(),'0',2);
		return year+month+day;
	},
	toShowDate: function(value,show_weekday)
	{
		if($.isDefined(value)){
			if(typeof(show_weekday)=="undefined") { show_weekday = false; }
			var weekday='';

			var year=value.substring(0,4);
			var month=value.substring(4,6);
			var day=value.substring(6,8);
			if(show_weekday) {
				weekday += ' [';
				weekday += Jade.Lang.Get('l_wday_'+Jade.DateTime.Weekday(new Date(year,month-1,day)));
				weekday += ']';
			}

			return day+'/'+month+'/'+year+weekday;
		}
		else{
			return '';
		}
	},
	toStoreTime: function(value) {
		var hour= Jade.Strings.PadLeft(value.getHours(),'0',2);
		var min= Jade.Strings.PadLeft(value.getMinutes(),'0',2);
		var sec= Jade.Strings.PadLeft(value.getSeconds(),'0',2);
		return hour+min+sec;
	},
	toShowTime: function(value) {
		return value.substring(0,2)+':'+value.substring(2,4)+':'+value.substring(4,6);
	},
	CurrentStoreDate: function(){
		return Jade.DateTime.toStoreDate(new Date());
	},
	CurrentDay: function(){
		return(new Date()).getDate();
	},
	CurrentMonth: function(){
		return (new Date()).getMonth()+1;
	},
	CurrentYear: function(){
		return (new Date()).getFullYear()
	},
	AddDays: function(date,days){
		days = parseInt(days, 10);
		return new Date(date.getFullYear(),date.getMonth(),date.getDate()+days);
	},
	MinutesToHour: function(value,sep){
		if(typeof(sep)=="undefined") { sep=''; }
		var sign=Jade.iif(value<0,'-','');
		var hours = Jade.Strings.PadLeft(Math.abs(parseInt(value/60,10)),'0',3);
		var minutes = Jade.Strings.PadLeft(Math.abs(value%60),'0',2);
		return sign+hours+sep+minutes;
	},
	HourToMinutes: function(value){
		
	},
	WeekNumber: function(date, includeYear=false){
		if($.isString(date)){
			date = $.to.date(date);
		}
		
		let weekday = $.datetime.Weekday(date);
		date.setUTCDate(date.getUTCDate() + 4 - weekday);
		
		let year = new Date(Date.UTC(date.getUTCFullYear(), 0, 1));
		let number = Math.ceil((((date - year) / 86400000) + 1)/7);
		
		if(!includeYear){
			return number;
		}
		else{
			return [number, year.getUTCFullYear()];
		}
	},
	WeekFormat: function(week, year) {
		var first = new Date(year, 0, 1+(week-1)*7);
		var weekday = first.getDay();
				if(weekday<=4){
			first.setDate(first.getDate()-weekday+1);
		}
		else{
			first.setDate(first.getDate()+8-weekday);
		}
		
		var last = new Date(first);
		last.setDate(last.getDate()+6);
		if(week==0){
			week = $.datetime.WeekNumber(first);
		}	
		
		return $.Strings.Format($.lng('week_format'),
			week, 
			first.getDate(), $.lng('month_'+(first.getMonth()+1)),
			last.getDate(), $.lng('month_'+(last.getMonth()+1))
		);
	},
	DaysInYear:function(year){
		var firstDay = new Date(year,0,1,0,0,0,0);
		var lastDay = new Date(year,11,31,0,0,0,0);
		return Jade.DateTime.DaysBetween(firstDay,lastDay)+1;
	},
	WeeksInYear:function(year){
		var firstDay = new Date(year,0,1,0,0,0,0);
		var lastDay = new Date(year,11,31,0,0,0,0);
		
		var weekDay = Jade.DateTime.UEWeekDay(firstDay.getDay());
		firstDay.setDate(firstDay.getDate()-weekDay);
		
		weekDay=Jade.DateTime.UEWeekDay(lastDay.getDay());
		lastDay.setDate(lastDay.getDate()+(6-weekDay));
		
		return Math.round((Jade.DateTime.DaysBetween(firstDay,lastDay)+1)/7);
	},
	DaysBetween: function(startDate, endDate){
		var startDate_ms = startDate.getTime();
		var endDate_ms = endDate.getTime();
		return Math.round(Math.abs(startDate_ms - endDate_ms)/(1000 * 60 * 60 * 24));
	},
	Ticks: function(){
		var date=new Date();
		return (date.getTime() * 10000) + 621355748000000000;
	}
}
Jade.LoadLibrary('DateTime',JadeDateTime);

Date.isLeapYear = function (year) { 
    return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0)); 
};
Date.getDaysInMonth = function (year, month) {
    return [31, (Date.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
};
Date.prototype.isLeapYear = function () { 
    return Date.isLeapYear(this.getFullYear()); 
};
Date.prototype.getDaysInMonth = function () { 
    return Date.getDaysInMonth(this.getFullYear(), this.getMonth());
};
Date.prototype.addMonths = function (value) {
    var n = this.getDate();
    this.setDate(1);
    this.setMonth(this.getMonth() + value);
    this.setDate(Math.min(n, this.getDaysInMonth()));
    return this;
};