set date with year confusion in javascript Date() - javascript

According to w3schools :
There are 4 ways of initiating a date:
new Date()
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
so when I try console.log(new Date(2015)); it gave me 1970-01-01T00:00:02.015Z ?

It thinks that 2015 is the amount of milliseconds you want.
You could try using a calculator to see how many milliseconds the year 2015 is equivalent to, but it would be bad to maintain.
You should use one of the other ways you listed:
new Date(dateString)
new Date('01/01/2015')
or
new Date(year, month, day, hours, minutes, seconds, milliseconds)
new Date(2015,0,1)

The syntax is:
console.log(new Date(milliseconds)
The docs on MDN state clearly:
Creates a JavaScript Date instance that represents a single moment in time. Date objects are based on a time value that is the number of milliseconds since 1 January, 1970 UTC.

You initialized it with a number, which actually means milliseconds or new Date(milliseconds).
Alternatively, you can do this:
console.log(new Date('01/01/2015'));
If what you want is the current time or year, you can get it with
var now = new Date();
console.log(now.getFullYear());
Finally, I recommend you to read MDN entries which are more accurate than W3Schools'.

Related

Get today's day from Date.now()

After getting the value from Date.now() I have tried extracting the date like that:
Date.now().getDate()
However, the getDate() function doesn't work on it. My whole application is based on dates that are retrieved from Date.now() and I need to get current day of the month from the Date.now() value. I have done some research and I can not find a solution to it.
Date.now() doesn't returns a Date instance:
The Date.now() method returns the number of milliseconds elapsed since
January 1, 1970 00:00:00 UTC
Being a number, it doesn't have .getDate() method. But, you can make use of Date without arguments to get a Date instance which has a .getDate() method:
If no arguments are provided, the constructor creates a JavaScript
Date object for the current date and time according to system
settings.
So this should work:
new Date().getDate(); // 3

Create a Date Object with the year only

I'm used to create Date objects by using the fourth syntax from MDN as new Date(year, month, day, hours, minutes, seconds, milliseconds); But lately I tried to set a Date object with only a year (as new Date(2017)) but as you could expect it was treated as a value and considered the year as a number of milliseconds.
Is there any way of still easily use the year as is without changing the syntax and expect a correctly set Date ?
Two solutions come to my mind:
(1) Set the year argument to 2017 and set the month argument to 0 when constructing the date:
let d = new Date(2017, 0);
console.log(d.toString());
The arguments will be treated as local time; month and day of month will be January 1; all time components will be set to 0.
(2) Specify "2017T00:00" as the first and only argument when constructing the date:
let d = new Date("2017T00:00");
console.log(d.toString());
According to current specs this is a valid format and browsers are supposed to treat it as local time. The behavior is same as that of previous example.
If you are passing a single parameter (number or string), then it is taken as per doc
value
Integer value representing the number of milliseconds since January 1,
1970 00:00:00 UTC, with leap seconds ignored (Unix Epoch; but consider
that most Unix time stamp functions count in seconds).
dateString
String value representing a date. The string should be in a format
recognized by the Date.parse() method (IETF-compliant RFC 2822
timestamps and also a version of ISO8601).
Also as per doc
If at least two arguments are supplied, missing arguments are either
set to 1 (if day is missing) or 0 for all others.
You can pass one more parameter as 0 or null (or the actual value you want to set)
new Date(2017,0);
Demo
var date = new Date(2017,0);
console.log( date );
You could pass null as second argument:
new Date(2017, null);
However, without knowing the details of how missing values are interpreted, what do you think happens now? Will the date be initialized with the current month, day, hour, etc? Or something different?
Better be explicit and pass all arguments, so that you know what the code is doing half a year later.
I have another suggestion. You can just create a regular date object and set it's year. So at least you know to expect what the rest of the Date object values are.
var year = "2014";
var date = new Date();
date.setFullYear(year);
// console.log(year) => Wed Dec 27 2014 16:25:28 GMT+0200
Further reading - Date.prototype.setFullYear()

Setting Date in the Future with Javascript

I am trying to generate a future date based on a previously set date, but I am getting strange output.
var today = new Date(),
expiration = (today.getTime() + (3*60*1000),
theFuture = new Date();
//setup future time
theFuture.setDate(expiration);
console.log(theFuture);
//outputs something like:
Tue Jan d) -2147483647 20:33:52 GMT-0500 (EST)
Why is the date malformed here?
Ultimately I want to compare the dates, but something isn't right here.
The argument to setDate is the day of the month, while the return value of getTime is the number of milliseconds since Jan 1 1970. So you're setting the day of the month to something like 1437007985574, which is almost 4 billion years in the future. You get a nonsensical result because the date formatting functions aren't designed to handle such large dates, and they're overflowing internally.
Since you're using getTime to get the time in milliseconds, you should use setTime to set it the same way:
var today = new Date(),
expiration = today.getTime() + (3*60*1000),
theFuture = new Date();
//setup future time
theFuture.setTime(expiration);
alert(theFuture);
getDate() returns day of the month (between 1 and 31). Thats why setDate results in a malformed date

How to get current date and time in websql?

I wanted to know how to get current date and time and what datatype should i use to store it in websql, sorry for being such a noob...
You could create a Date object and store the numeric value of the date as the number of milliseconds since January 1, 1970, 00:00:00 UTC (negative for prior times).
var today = new Date();
today.getTime(); // returns "1337217238392"
Storing the milliseconds will allow you to read and parse however you like later. Its worth mentioning to check out date.js and moment.js

Init javascript Date object

I would like to retrieve a Javascript object Date for new year. I want to user new Date(); object and init it to the 2009-01-01 (it's for a countdown).
Thanks
The month part of the construct is an enum, so it's always themonthyouwant -1. And are you sure you want to count down to 2009? oh well...
var newYears = new Date(2009, 0, 1);
From http://www.w3schools.com/js/js_obj_date.asp, you can init your js date object with
var date= new Date(year, month, day, hours, minutes, seconds, milliseconds);
You may also use date.setFullYear(year,month,day) method if your date object has been created before. Please note that month is between 0 and 11 just like what David Hedlund said.
use...
dateVariable.setTime(Date.parse('1/1/2009 12:00 AM'));

Categories

Resources