INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

convert date function

convert date function

(OP)
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

RE: convert date function

Do you want to do this in VBScript or Javascript?

Basically, you get the day, month and year as numbers and reassemble it the other way round. There is no built in function with fancy formatting to do this.

RE: convert date function

Yep, just a Split function and some concatenation.

CODE

dim olddate
dim newdate
dim arytmp

olddate = "01/12/2016"
arytmp = Split(olddate,"/")

' Array is zero based
newdate = arytmp(2) & "-" & arytmp(1) & "-" & arytmp(0)

msgbox newdate 

Swi

RE: convert date function

(OP)
Thank you both very much. It's working.

RE: convert date function

Nice one Chris. Have a star.

Swi

RE: convert date function

(OP)
Ok, I think I need more assistance here (to do with what Chris was saying).

I need to know whether or not the date/time chosen is BST or GMT.

At the moment this is my date/time value:

CODE

"<PickupTime>" & NewDate & "T" & PickupHour & ":" & PickupMinute & ":00+00:00</PickupTime>" 

The NewDate field is now formatted correctly using Swi's function, an example of which is:

2012-11-18 = 18 November 2012.

Now each date will only ever be GMT or BST and this will of course depend on the time, date and year.

One date may be BST in 2012 but GMT in 2016.

Is there a protocol to automatically choose the correct time zone based on the date/time?

If the time/date is BST I'll need to plus 1 hour to the time format, like so:


1:23pm on 18 November 2012 (GMT) is: 2012-11-18T13:23:00+00:00

2.53am on 02 June 2012 (BST) is 2012-06-02T02:53:00+01:00


Dear oh dear this is complicated.

RE: convert date function

What is being fed to you for you to figure out if it is GMT or BST?

Swi

RE: convert date function

(OP)
2012-11-18T13:23:00+00:00

This is the live state of my date time field.

Alternatively I can use:

18/11/2012

I'll need to establish the time zone based on one of those two date time formats

RE: convert date function

(OP)
If I can basically establish which time zone a date is using 18/11/2012 then I can adjust the longer time/date format accordingly.

RE: convert date function

Correct but is there any indicator of whether the data is in GMT or BST?

Swi

RE: convert date function

(OP)
Well no. My date is in that format (from my website) and I have to establish whether or not that date is gmt or bst

RE: convert date function

(OP)
So basically I will be given a date by my customer, for example:

23/12/2015

I then need to establish whether or not that date is GMT or BST (again, the cut off point changes every year so I presume there is a function that checks this automatically).

Once I know whether or not any given date is GMT or BST I can then adjust my full date/time value (which is straightforward).

RE: convert date function

Use dateValue() to convert the string

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: convert date function

(OP)
Sorry Chris, I know you are trying to encourage me to find a solution myself but I cannot find any specific examples where you provide a date and the program returns either GMT or BST.

The only DateValue() things I can see online are how to convert or display the date.

RE: convert date function

That is what FormatDateTime() does, you give the function a date string and a formatand if the link to the DevGuru examples is in my first post and if you set the localeID (also in first post) you can set the preferred format rather than it being the SERVER locale, which may or may not be the visitors locale.

Links for locale settings are also in first post.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: convert date function

(OP)
Ok let me give that at try... thanks.

RE: convert date function

(OP)
Chris I'm sorry I cannot get my head around this.
I understand you can set the locale ID to UK but the devguru link you provided does not show how to display the date with the time zone attached.

I basically need to submit my date/time value in standard XSD format.

yyyy-mm-ddThh:mm:ss+tzdst

My customers are booking a taxi and they are basically choosing the pickup date for the taxi pickup. It could be any date in the future and the last thing I need to do is establish is whether or not the date they have chosen is GMT or BST (UK Dates Only).

I have converted the date from my website (25/12/2015) to (2015-12-25) and I have my time values already (in minutes and hours).

I'm currently submitting my XML date/time like so:

CODE

<PickupTime>" & NewDate & "T" & PickupHour & ":" & PickupMinute & ":00+00:00</PickupTime> 



If the date is GMT I will need to place +00:00 at the end of the value, if the date is BST I will need to place +01:00 at the end of the value.

Is there any chance you can give me a solution. I've tried my best to make things as clear as possible here.

Here are the notable elements of the code:

CODE

OldDate = "25/12/2015"
PickupHour = "15"
PickupMinute = "30"

AryTmp = Split(OldDate,"/")
NewDate = AryTmp(2) & "-" & AryTmp(1) & "-" & AryTmp(0)

stringXML = "<PickupTime>" & NewDate & "T" & PickupHour & ":" & PickupMinute & ":00+00:00</PickupTime>" 



RE: convert date function

(OP)
This link says you need to use some JavaScript

Link

RE: convert date function

Quote:

If the date is GMT I will need to place +00:00 at the end of the value, if the date is BST I will need to place +01:00 at the end of the value.

Then you need to use javascript to display the time and date in the locale of the requesting client.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: convert date function

(OP)
Sorry Chris, too many cryptic clues. I'm stressed out as it is. I'll look elsewhere. Thanks for trying.

RE: convert date function

Ok, from one of my examples site (not currently live)

CODE --> 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

RE: convert date function

(OP)
Something like this but I'll have to change the Locale ID. Can you help me get this working?

Link

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close