This question already has answers here:
Creating java date object from year,month,day
(6 answers)
java.util.Date and getYear()
(12 answers)
Closed 2 years ago.
Currently I am trying to convert a date calculation from javascript to java, since I want to use it for Android development. So in javascript I was using the method date.valueOf(), which converts the date to the milliseconds that passed since January 1, 1970. In java the method with the same funcionality is called date.getTime(). In the java and javascript documentation the description is exactly the same, but when I am inserting the same date, I am getting two completely different values.
For example:
Java getTime()
Date date = new Date(2011, 10, 1);
System.out.println(date.getTime()); //prints: 61278249600000
Javascript valueOf()
const date = new Date(2011, 10, 1);
console.log(date.valueOf()); //prints: 1320102000000
So my questions are:
Why is this happening? (or what did I wrong?)
How can I correct the code?
It would be awesome if somebody can help me.
The discrepancy isn't between Java's getTime and JavaScript's valueOf, it's the Java Date constructor. The year parameter is years since 1900. So the equivalent Java code would be:
Date date = new Date(110, 10, 1);
// ^−−− 2010 - 1900
This is one of the many reasons not to use the java.util.Date class.
Related
This question already has answers here:
Is the Javascript date object always one day off?
(29 answers)
Closed 2 years ago.
I want to represent the first of a given month, say 1st of Septemeber 2020. When I do this in my console, I get the below output:
$ node
> new Date(2020, 8, 1)
2020-08-31T23:00:00.000Z
How come it returns 2020-08-31 instead of 2020-08-01? What am I doing wrong?
The output
2020-08-31T23:00:00.000Z
is ISO format, and the Z shows the date is in UTC. When you call the Date constructor, it is using your browser timezone by default, which I can assume is +01:00. Moreover, month indice in the constructor is zero-based.
If what you want is the beginning of the month in UTC, you can do:
d = new Date(Date.UTC(2020, 8, 01))
d.toISOString()
"2020-09-01T00:00:00.000Z"
try
new Date(2020, 7, 1)
because date object is count from 0-11 no 1-12
This question already has answers here:
Convert a Unix timestamp to time in JavaScript
(34 answers)
Closed 5 years ago.
Pretty straightforward issue, but I haven't found any information on this after looking around a bunch.
Essentially, I want to convert a series of UTC dates (e.g. "1505952000") into regular date strings (e.g., "9/21"), to use today as an example.
For some reason, however, .toDateString() is erroring out as "not a function" when I try to run it. How do I make this simple conversion work?
Here's my code, and I've console-logged day.dt to ensure that it's a valid UTC date when it runs:
let dt = day.dt.toDateString();
UTC var stored in seconds from Jan. 1, 1970.
So to convert it back to the local date time, use this snippet:
var d = new Date(0);
d.setUTCSeconds(1505952000);
console.log(d);
OR
var d = new Date(1505952000 * 1000); // Because this constructor takes miliseconds.
console.log(d);
This question already has answers here:
Get Epoch in C# for GMT
(2 answers)
Closed 5 years ago.
Is there any equivalent of Javascript new Date().valueOf() in C#?
I tried using DateTime.Now.Ticks in c#, but both are different.
I need this because, I'm writing some serverless aws lambda code where they are supporting both nodeJs and C# code.
So, I don't want to get any conflict with datetime in future.
In future, I may query on the datetime values.
new Date().valueOf returns the millis since january 1, 1970 UTC, you can use this C# code:
DateTime startDt = new DateTime(1970, 1, 1);
TimeSpan timeSpan = DateTime.UtcNow - startDt;
long millis = (long)timeSpan.TotalMilliseconds;
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 6 years ago.
There is simpleDateFormat available in java,similarly is there any way to format the date in JavaScript
What is the best equivalent way to perform date formats in JavaScript.
I think In javascript we don't have straight forward functions to get dateformats, to get the exact format you need to call differnt functions on Date object(in javascript).
but in jqueryUI we have formatDate().
var d = new Date(2016, 0, 30) // 30 Jan 2016
alert( formatDate(d) ) // '30.01.16'
This question already has answers here:
Date vs new Date in JavaScript
(8 answers)
Closed 8 years ago.
I have been using this for comparing dates on IE and other browser but
could anyone explain me how this works differently?
Date() and Date("2014-08-05T07:47:02.051Z")
Have different result on:
new Date() and new Date("2014-08-05T07:47:02.051Z")
And is there any problem that will pop?
Edit
When I execute
console.log(Date(expiryDate),Date())
It just gives the same value.
The ES5 spec makes it pretty clear:
When Date is called as a function rather than as a constructor, it returns a String representing the current time .
If you want an actual Date instance you'll have to call it with the new operator. If you are happy with just a string representation of the date you don't need to bother with the new operator.
In other words, if you need to be able to call methods on your date (such as getTime or setHours) you'll need to use new Date but if you only want a string you can just use Date.