Javascript UNIX time conversion - javascript

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.

Related

Javascript Date inconsistency

On my node server, I receive time value from the client in epoch format (milliseconds since Jan 1 1970). I feed it into the Date() object and print it like so:
var d = new Date(epochTime);
var year = d.getFullYear();
var mo = d.getMonth();
var day = d.getDay();
console.log("ISO:" + d.toISOString() + " year:" + year + " mo:" + mo + " day:" + day);
Now, I get weird inconsistency from the date object.
E.g. for the value of "1437386620207.58" - this is what above console.log prints:
ISO:2015-07-20T10:03:40.207Z year:2015 mo:6 day:1
Huh? Why are those dates so different?
Two problems in your code:
Months are zero-based in Javascript, i.e. 0 is January and 11 is December.
getDay() returns the day of the week. That should be getDate() instead to return the day of the month.

Selenium IDE - How to create a JavaScript function relating to todays date?

I am quite new to using Selenium IDE. I am trying to create a suite of regression scripts to use within our department. I am trying to figure out how to enter todays date + 180 days into a field using a JavaScript function.
Could anyone enlighten me as how to write that function? I am in the process of learning JavaScript but it is a long old process!
If you need any more information, just ask.
Thanks for your help in advance!
Dan
Simple JS solution will be something like,
var now = new Date();
var date = new Date(new Date().setMilliseconds(now.getMilliseconds() + (24000 * 180 * 60 * 60)));
console.log(now);
console.log(date);
This should print something like,
Mon Jan 19 2015 12:26:21 GMT-0500 (EST)
Sat Jul 18 2015 12:26:21 GMT-0400 (EDT)
Thanks for the replies. I have managed to get what i needed from the following JS:
Selenium.prototype.doTypeTenantDate = function(locator){
var dates = new Date();
dates.setDate(dates.getDate() + 180);
var day = dates.getDate();
if (day < 10){
day = '0' + day;
}
month = dates.getMonth() + 1;
if (month < 10){
month = '0' + month;
}
var year = dates.getFullYear();
var prettyDay = day + '/' + month + '/' + year;
this.doType(locator, prettyDay);
};

Javascript Concatenate dates and time together

I have this code which generates the current date + time in javascript:
var date_variable = new Date();
var year = date_variable.getFullYear();
var month = date_variable.getMonth() + 1;
var day = date_variable.getDate();
var hour = date_variable.getHours();
var minutes = date_variable.getMinutes();
var seconds = date_variable.getSeconds();
var full_date = year + month + day + hour + minutes + seconds;
console.log(year);
console.log(month);
console.log(day);
console.log(hour);
console.log(minutes);
console.log(seconds);
console.log(full_date);
Everything in console displays fine, except when it comes to the full_date variable. This is what's displayed in console:
2014
8
27
10
53
10
2122
My question is, why does the last output not combine my date + time into a single string?
Thanks
You need to concatenate the numbers with a string first.
var full_date = year +""+ month +""+ day +""+ hour +""+ minutes +""+ seconds;
Each of the properties you are accessing return a number. When you add them together with the + operator, you are just adding numbers together.
If you substitute the variables used in full date, it would look something like:
var full_date = 2014 + 8 + 26 + . . .
All you have to do is put a strings in the expression and you will get what you want.
In all honesty though, you should use Date.toLocalDateString() or Date.toLocalTimeString() if the format works for you. You can read the documentation about them on MDN's Date reference page.

Calculate the Date based on Current Date and No of Days using Javascript/Jquery

I need a help..
I have a Current Date and No of days column.
When i enter number of days,i should add current date plus no of days entered.
For example,
todays date 5th jan + 20(no of days) = 25th Jan 2011 in another column.
Kindly help me.
Thanks in Advance.
Date.js is fantastic for this.
Date.today().add(5).days();
As you are learning JavaScript you may find the w3schools site useful for simple examples of objects and functions that are exposed and how they may be used.
http://www.w3schools.com/jsref/jsref_obj_date.asp
You can calculate the date as follows:
var d = new Date(); // Gets current date
var day = 86400000; // # milliseconds in a day
var numberOfDays = 20;
d.setTime(d.getTime() + (day*numberOfDays)); // Add the number of days in milliseconds
You can then use one of the various methods of displaying the date:
alert(d.toUTCString());
You could do something like
Date.today().add(X).days();
Where X is the number of days the user has entered.
You can add dates like this in js:
var someDate = new Date();
var numberOfDaysToAdd = 6;
someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
var month = someDate.getMonth() + 1; //Add 1 because January is set to 0 and Dec is 11
var day = someDate.getDate();
var year = someDate.getFullYear();
document.write(month + "/" + day + "/" + year);
See this p.cambell's answer here: How to add number of days to today's date?

Can we get timezone of a user based on GMT

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

Categories

Resources