Javascript Date valueOf equivalent in C# [duplicate] - javascript

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;

Related

Difference between javascript date.valueOf() and java date.getTime() [duplicate]

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.

How to handle JWT.exp time? [duplicate]

This question already has answers here:
Convert a Unix timestamp to time in JavaScript
(34 answers)
Closed 4 years ago.
console.log('DEBUG::+jwtDecode(token).exp', +jwtDecode(token).exp); //1534820211
console.log('DEBUG::try', new Date(+jwtDecode(token).exp).toISOString());
//DEBUG::try 1970-01-18T18:20:20.211Z
I'm having a token with value 1534820211 and when I try to convert it using toISOString() it gives me year 1970-01-18T18:20:20.211Z.
But when I decode the same token at jwt.io, and mouse hover over exp, it shows 2018-08-21.... which is huge difference. I have also tried to pass jwtDecode(token).exp into moment and using format, still return me datetime in 1970xxxx.
moment(jwtDecode(token).exp).format();
The value you have is seconds from epoch.
JavaScript Date constructor (and moment function too) accepts value in milliseconds from epoch. Multiply the number by 1000, and your code should work fine:
var exp = 1534820211 * 1000;
console.log(new Date(exp));

Convert UTC date seconds to Date [duplicate]

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);

Equivalent code in python (time) [duplicate]

This question already has answers here:
Converting unix timestamp string to readable date
(19 answers)
Closed 6 years ago.
Javascript code:
var date = new Date(1466278504960);
return: Sat Jun 18 2016 20:35:04 GMT+0100 (WEST)
How can I convert the same number to date in python ?
When I use
datetime.datetime.fromtimestamp(int("1466278504960")).strftime('%Y-%m-%d %H:%M:%S'))
I receive this error: ValueError: year is out of range
datetime.datetime.fromtimestamp will do this, but you need to divide the value by 1000 first (the numeric value you give and JavaScript's Date expects is in milliseconds since the epoch, where Python's API takes a floating point seconds since the epoch):
from datetime import datetime
date = datetime.fromtimestamp(1466278504960 / 1000.)
That makes the raw datetime object; if you want it formatted the same, you should take a look at datetime object's strftime method.
It's almost the same. You just have to convert the units.
Date from javascript specifies the number in milliseconds, in other words, expects a number in millisenconds as a parameter. When the python date takes seconds.

How the date can be formatted in JavaScript? [duplicate]

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'

Categories