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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if date is BST or GMT, timezone help

Status
Not open for further replies.

axLW

Programmer
Feb 6, 2015
110
GB
I need a function to check whether a given date falls within British Summer Time or Greenwich Mean Time.

For example:

24/10/2015 is the last day of British Summer Time
25/10/2015 is the first day of Greenwich Mean Time

In a perfect world, my function would look something like this:

Code:
MyDate = "24/10/2015"
<check if MyDate falls within UK BST or GMT>
Result = "This date is BST"







 
And what have you tried so far?

Look at the Date object in Javascript.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Already posted this in the ASP thread so may as well post it here as well


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
 
Thanks Chris. I appreciate your efforts but this does not resolve my problem.

I have been very specific as to what I need.

This code does not allow me to enter a date and then calculate whether or not that date falls within UK British Summer Time or Greenwich Mean Time.

Any other suggestions are welcome.
 
HOW can you EVEN KNOW if any particular time is in a particular time zone UNLESS you already KNOW what time zone the user is in.


Time is arbitrary, let's say at this moment it is 22:25 on Monday the 10th of August can YOU tell me what time zone that is and whether Daylight Saving adjustment is in force or not?
(that is not the actual time here by the way).

The problem is that you are not really understanding what you are asking for and trying to get at the wrong way.

You cannot tell whether any time, given ONLY the time and date, is GMT, BST, EST, PST, CET or BBC, you EITHER have to know the hour offset (+ or -) from GMT is OR the timezone the end user is in.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
My customer is booking a taxi.
The taxi service will be provided in the UK.

If they tell me their flight arrives at 22:25hrs on 23rd of October 2015, I can safely say that date falls within British Summer Time for the year 2015. If they tell me their flight arrives on 4th November 2015, I know that in the UK we would have reverted back to GMT. This is all I need to know!

I am not concerned about what the time is wherever the customer is.

 
I would think that a good place to start might be a web reference like the following - where the Longitude and Latitude values to be used would represent your final location (such as Heathrow - not the current one):
How to get a time zone from a location using latitude and longitude coordinates?
which in turn references: General Information About Time Zones

Good Luck
 
This is depressing. I give up.
 
Chris do you at least understand what I'm trying to do now?
 
If they tell me their flight arrives at 22:25hrs on 23rd of October 2015

I think you can safely assume that daylight savings time will have already have been allowed for in the timing that airlines provide. So if an airline says 15:30 at Heathrow they ARE ALREADY using whatever local time it is going to be AT Heathrow on that date.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Chris why are you questioning what I'm doing. It is a requirement of the system I'm using that I must differentiate between the two.

Can you please not respond to my posts anymore. You are reluctant to provide a solution (which is fair enough) but worse than that is that you don't seem to understand what I'm asking plus you are now questioning why I'm asking it in the first place.

You have helped me in the past and for that I'm thankful but on this post in particular you've been a pain the a**!

 
Perhaps this answers your question? :-
The important thing to note is
In the UK the clocks go forward 1 hour at 1am on the last Sunday in March, and back 1 hour at 2am on the last Sunday in October.
take your date and do the math.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top