Hi guys,

I'm new with javascript, so I need help from you guys.
Please help me to change date format to yyyy-mm-dd.
I've tried few ways but failed.

Please download the script here http://www.javascriptkit.com/script/script2/datetimepick.zip

Thank you :)

Recommended Answers

All 4 Replies

It doesn't behave as the documentation says it should.

Both sample date formats "yyyyMMdd" and "yyyyMMMdd" give "undefined".

Try adding the following lines to function FormatDate :

else if (this.Format.toUpperCase()=="YYYYMMDD")
		return (this.Year+DateSeparator+(this.Month+1)+DateSeparator+pDate);

Airshow

thanks for your reply sir,

but i would like to reconfirm with u..
after i add the following codes above, everything was fine.. i pick any date and the date format inserted correctly but there's nothing happen (no calendar pop-up) when i clicked the calendar to re-pick the date.

pls advice. TQ

I think the best advice would be to find a better datepicker but if you want to stick with that one, then try replacing function NewCal(){...} with :

if(!String.prototype.trim) {
	String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
}

function NewCal(pCtrl,pFormat,pShowTime,pTimeMode) {
	Cal = new Calendar(dtToday);
	if ((pShowTime!==null) && pShowTime) {
		Cal.ShowTime=true;
		if ((pTimeMode!==null) &&((parseInt(pTimeMode,10)===12)||(parseInt(pTimeMode,10)===24))) {
			TimeMode=pTimeMode;
		}
	}
	if (pCtrl!==null) { Cal.Ctrl=pCtrl; }
	if (pFormat!==null) { Cal.Format=pFormat.toUpperCase(); }
	
	exDateTime=document.getElementById(pCtrl).value.trim();

	if (exDateTime!=="") {//Parse Date String
		var parserArray = exDateTime.split(/\s+/g);
		var exDateArray = (parserArray[0]) ? parserArray[0].split(DateSeparator) : '';
		var exTimeArray = (parserArray[1]) ? parserArray[1].split(':') : '';
		var strMonth;
		var strDate;
		var strYear;
		var intMonth;
		var YearPattern;
		//extract date, month and year
		switch (Cal.Format.toUpperCase()) {
			case "DDMMYYYY":
			case "DDMMMYYYY":
				strDate=exDateArray[0];
				strMonth=exDateArray[1];
				strYear=exDateArray[2];
			break;
			case "MMDDYYYY":
			case "MMMDDYYYY":
				strDate=exDateArray[1];
				strMonth=exDateArray[0];
				strYear=exDateArray[2];
			break;
			case "YYYYMMDD":
			case "YYYYMMMDD":
				strDate=exDateArray[2];
				strMonth=exDateArray[1];
				strYear=exDateArray[0];
			break;
		} 
		//... month
		if (isNaN(strMonth)) { intMonth=Cal.GetMonthIndex(strMonth); }
		else { intMonth=parseInt(strMonth,10)-1; }
		if ((parseInt(intMonth,10)>=0) && (parseInt(intMonth,10)<12))  { Cal.Month=intMonth; }
		//... date
		if ((parseInt(strDate,10)<=Cal.GetMonDays()) && (parseInt(strDate,10)>=1)) { Cal.Date=strDate; }
		//... year
		YearPattern=/^\d{4}$/;
		if (YearPattern.test(strYear)) { Cal.Year=parseInt(strYear,10); }
		//parse time
		if (Cal.ShowTime) {
			Cal.SetHour(exTimeArray[0]);
			Cal.SetMinute(exTimeArray[1]);
			Cal.SetSecond(exTimeArray[2]);
		}
	}
	winCal=window.open("","DateTimePicker","toolbar=0,status=0,menubar=0,fullscreen=no,width=195,height=245,resizable=0,top="+cnTop+",left="+cnLeft);
	docCal=winCal.document;
	RenderCal();
}

and replacing function FormatDate(){...} with:

function FormatDate(pDate) {
	var rtnStr;
	switch(this.Format.toUpperCase()) {
		case "DDMMYYYY":  rtnStr = [pDate, this.Month+1, this.Year].join(DateSeparator); break;
		case "DDMMMYYYY": rtnStr = [pDate, this.GetMonthName(false), this.Year].join(DateSeparator); break;
		case "MMDDYYYY":  rtnStr = [this.Month+1, pDate, this.Year].join(DateSeparator); break;
		case "YYYYMMDD":  rtnStr = [this.Year, this.Month+1, pDate].join(DateSeparator); break;
		case "MMMDDYYYY": rtnStr = [this.GetMonthName(false), pDate, this.Year].join(DateSeparator); break;
		case "MMDDYYYY":  rtnStr = [this.Month+1, pDate, this.Year].join(DateSeparator); break;
	}
	return rtnStr;
}

Airshow

commented: very helpful. thank you very much! +1

thanks for your help sir! really appreciate it!

Actually i've searched few datetime picker, but most of them got problem when i tried to integrate with my php script, e.g. pls refer this post http://www.daniweb.com/forums/post1438937.html#post1438937

but anyway, thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.