Can we get timezone of a user based on GMT - javascript

i came across this code which shows the current time of the user based on his timezone. Based on this can we tell if he is on IST or EST.plz help
<script type="text/javascript">
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes < 10)
minutes = "0" + minutes
document.write("<b>" + hours + ":" + minutes + " " + "</b>")
</script>

i wanted to display the time along with timezone.for example 17:35 IST
var now = new Date();
localtime = now.toTimeString();
which will return something like "12:21:44 GMT-0400 (EDT)"

You cannot tell what timezone he is in - just what time they are in based on GMT - use this useful function to get your info though -> http://www.pageloom.com/automatic-timezone-detection-with-javascript

Related

How to get time from date object without time zone in javascript?

I have Date object like
Date {Mon Aug 17 2015 05:45:23 GMT+0530 (IST)}
i want only time without GMT timezone and IST.I want only time like 05:45:23.thanks
You can use moment javascript library to format and process your dates.
The alternative option is manually accessing date hours, minutes and seconds.
Check the Date Reference.
You can build a string by using:
var formattedDate = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
Using pure JavaScript:
var d = new Date(),
hours = d.getHours(),
mins = d.getMinutes(),
secs = d.getSeconds(),
time = hours + ':' + mins + ':' + secs;
console.log(time);
You can create your own prototype extending the Date object.
Date.prototype.extractTime = function(){
var h = this.getHours();
var m = this.getMinutes();
var s = this.getSeconds();
return h + ":" + m + ":" + s;
}
Usage:
var d = new Date();
d.extractTime(); // outputs current time of your clock..

how to convert date object of js to time in h:i A (7:30 A.M) format

I have a javascript object of date and its value is:
Thu Dec 18 2014 07:29:44 GMT+0500 (PKT)
Now I want to get the time from above data object in following format:
7:29 A.M
How can I do that using javascript code.
Please Help!!
You can do a little string addition by doing the following:
var result = "";
var date = new Date();
var hours = date.getHours();
var minutes = date.getMinutes();
var amORpm = hours > 12 ? "P.M" : "A.M";
if(hours > 12) hours -= 12;
result = hours + ":" + minutes + " " + amORpm;
console.log(result); // Will get you your desired format

Javascript display various time zones

The following script should be displaying the current local time based on the offset of -10 (Hawaii), but it's not working.
Can't figure out where I'm going wrong.
<h3>Current Time in Arizona is
<script type="text/javascript">
<!--
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes < 10)
minutes = "0" + minutes
var suffix = "AM";
if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
}
if (hours == 0) {
hours = 12;
}
document.write("<b>" + hours + ":" + minutes + " " + suffix + "</b>")
//-->
</script>
</h3>
First of all, the code you've shown just returns the current local time. It doesn't even attempt to change it for a specific time zone.
Secondly, you need to read the timezone tag wiki. In particular, read the section titled "Time Zone != Offset".
Now it just so happens that Arizona and Hawaii don't currently use daylight saving time, so you could adjust by offset if those were your only two concerns. But I'm sure you are looking for a more general solution.
To do it properly, you will need a library that implements the IANA time zone database. I list several of them here. For example, here is an example of displaying the current time in Los Angeles using moment.js with the moment-timezone plugin:
moment().tz("America/Los_Angeles").format("h:mm a")
If you're just looking for a quick and easy way to put a clock on your web site for a particular time zone, then I recommend using the free solution offered by timeanddate.com.
Write a function to move a Date by some offset in minutes/your choice
function offsetDate(offsetMinutes, d) {
if (d) d = new Date(d);
else d = new Date();
if (offsetMinutes) d.setUTCMinutes(d.getUTCMinutes() + offsetMinutes);
return d;
}
offsetDate(-10*60); // Thu Sep 05 2013 12:03:06 GMT+0100 (GMT Daylight Time)
Now use UTC functions to get the time

Javascript UNIX time conversion

Hi I have the following problem I am converting this UNIX timestamp to javascript string for date: here is the jsfiddle for it http://jsfiddle.net/tczeU/ and as everyone can see the date is 2 6 2013 13:15:44 so the problem is that this number 1373969744 in UNIX timestamp converter is Tue, 16 Jul 2013 10:15:44 GMT The problem are that there are 14 days bwtween the two dates where does I go wrong? Please help me to convert this date. The code is as in the fiddle:
var date = new Date(1373969744*1000);
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var year = date.getFullYear();
var day = date.getDay();
var month = date.getMonth();
var string =day + " " + month + " " + year + " " + hours + ':' + minutes + ':' + seconds;
$("#view").html(string);
and the html:
<div id="view"></div>
So no errors there. Please help. Any help will be apreciated!
You are using the wrong function to get the day of the month. You are using the function that returns the day of the week, hence the 2 since it's a Tuesday. Check out http://www.w3schools.com/jsref/jsref_obj_date.asp
You need to change .getDay to .getDate and it will work just fine. Or at least it did for me using your jsFiddle link.
Also, don't forget to add one to your month so it has Jul as 7th month instead of 6th like you have it now.

How to format a dateTime

Okay I have the following problem. I want to get the current dateTime and then want do check if a date that I enter is bigger than the current DateTime. The format of my dateTime should look like this.
03/11/2012 09:37 AM
Here is the function how I get the current DateTime.
function getCurrentDateTime()
{
var currentTime = new Date()
// Date
var month = currentTime.getMonth() + 1;
if (month < 10){
month = "0" + month;
}
var day = currentTime.getDate();
var year = currentTime.getFullYear();
// Time
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10){
minutes = "0" + minutes;
}
if(hours > 11){
var dateString = month + "/" + day + "/" + year + " " + hours + ":" + minutes + " " + "PM";
test = new Date(dateString);
return dateString ;
} else {
var dateString = month + "/" + day + "/" + year + " " + hours + ":" + minutes + " " + "AM";
return dateString;
}
}
As you can see how it gives back a string. But when I want to covert it to a date with this function. I get this format Fri May 11 2012 09:37:00 GMT+0200 (Romance Daylight Time)
date = new Date(dateString);
And with this I can't calculate.
Could anybody help me how I can get the current date in this format so that I can do the check?
Kind regards.
Javascript provides very limited functionality for working with dates out of the box. Use an external library like momentjs.
For example, your function would be reduced to
var stringDate = moment().format("DD/MM/YYYY HH:mm A");
And you could convert that and compare it to the current time with
var earlierDate = moment(stringDate, "DD/MM/YYYY HH:mm A");
if (earlierDate.valueOf() < moment().valueOf()) {
// earlier indeed
}
datejs is another lib for solving date-manipulation problems

Categories

Resources