Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert date function 2

Status
Not open for further replies.

axLW

Programmer
Feb 6, 2015
110
GB
Hello, I have a date

dd/mm/yyyy

I need to convert it to standard XSD format:

yyyy-mm-dd

So:

01/12/2016 needs to become 2016-12-01

Have no idea how to change the order of characters in a string.

Thanks

 
Sorry Chris, too many cryptic clues. I'm stressed out as it is. I'll look elsewhere. Thanks for trying.
 
Ok, from one of my examples site (not currently live)

HTML:
<head>
<script type="text/javascript">

function getDayName(index,long,firstDay) {
// returns name of day
// index = day code 0 to 6 : REQUIRED
// long = true: return long name (Sunday): false = return short (Sun)
// firstDay = first day of the week 0 = Sunday (default); 1 - 5 = Monday; 6 = Saturday
if (firstDay == undefined) {
	firstDay = 0;
	}
if (long == undefined) {
	long = true;
	}

var tmp = ""
var Name = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var shortName = new Array("Sun","Mon","Tues","Wed","Thurs","Fri","Sat");
	if (firstDay > 0 && firstDay < 6) {
		tmp = Name.shift();
		Name.push(tmp);
		tmp = shortName.shift();
		shortName.push(tmp);
	} else if (firstDay == 6) {
		tmp = Name.pop();
		Name.unshift(tmp);
		tmp = shortName.pop();
		shortName.unshift(tmp);
	}

	if (long) {
		return Name[index];
	} else {
		return shortName[index];
	}

}

function addDateSuffix(day) {
if (day > 0 && day < 32) {
	if (day == 1  | day == 21 | day == 31) {
		return day.toString() + "st";
	} else if (day == 2  | day == 22) {
		return day.toString() + "nd";
	} else if (day == 3  | day == 23) {
		return day.toString() + "rd";
	} else {
		return day.toString() + "th";
	}
} else {
	return "";
}
}

function getMonthName(index,long) {
if (long == undefined) {
	long = true;
}
var Name=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var shortName=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	if (long) {
		return Name[index];
	} else {
		return shortName[index];
	}
}

function dateFormat(formatString,dateValue) {
var retVal = formatString;
var date = new Date(dateValue);
var fmt = formatString;

var day = date.getDay();
var d = date.getDate().toString();
var m = (date.getMonth()+1).toString();

if (fmt.indexOf("YYYY") > -1 | fmt.indexOf("yyyy") > -1 ) {
		retVal = retVal.replace(/YYYY/i,date.getFullYear().toString());
	} else if (fmt.indexOf("YY") > -1 | fmt.indexOf("yy") > -1) {
		retVal = retVal.replace(/yy/i,date.getFullYear().toString());
}
if (fmt.indexOf("DD") > -1) {
		retVal = retVal.replace(/DD/,getDayName(date.getDay()) + " " + addDateSuffix(date.getDate()));
	} else if (fmt.indexOf("D") > -1) {
		retVal = retVal.replace(/D\b/,getDayName(date.getDay(),false) + " " + addDateSuffix(date.getDate()));
	} else if (fmt.indexOf("dd") > -1) {
		if (d.length == 1) {
			d = "0" + d;
	}
	retVal = retVal.replace(/dd/,d);
	} else if (fmt.indexOf("d") > -1) {
	retVal = retVal.replace(/d\b/,d);
}

if (fmt.indexOf("MM") > -1) {
		retVal = retVal.replace(/MM/,getMonthName(date.getMonth()) + " ") ;
	} else if (fmt.indexOf("M") > -1) {
		retVal = retVal.replace(/M\b/,getMonthName(date.getMonth(),false) + " " );
	} else if (fmt.indexOf("mm") > -1) {
		if (m.length == 1) {
			m = "0" + m;
	}
	retVal = retVal.replace(/mm/,m);
	} else if (fmt.indexOf("m") > -1) {
	retVal = retVal.replace(/m/,m);
}
return retVal;
}
</script>
</head>

<h2 class="italic">Javascript dateFormat Function</h2>


<script type="text/javascript">
document.write(dateFormat("DD MM YYYY",Date()));
document.write("<br>");
document.write(dateFormat("dd MM yy",Date()));
document.write("<br>");
document.write(dateFormat("d/m/yy",Date()));
document.write("<br>");
document.write(dateFormat("dd mm yy",Date()));
document.write("<br>");
document.write(dateFormat("D M yy",Date()));
document.write("<br>");
document.write(getDayName(2,6,true));
document.write("<br>");
document.write(addDateSuffix(31));
document.write("<br>");

</script>



Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Something like this but I'll have to change the Locale ID. Can you help me get this working?

Link
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top