C# timestamp to js

595
January 03, 2017, at 05:18 AM

I am trying to recreate a C# timestamp in javascript but am not successfull :-/

Here is how my C# timestamp looks like:

DateTime now = DateTime.UtcNow;
now = new DateTime (now.Year, 1, 1);
int yearDay = (int)(now.Subtract (new DateTime (1970, 1, 1))).TotalSeconds;

This is the return: 1483228800

I have tried this in javascript:

var now = new Date();
now = new Date(now.Year, 1, 1);
var yearDay = ((now.(new Date(1970, 1, 1))).TotalSeconds);

But that gives me an error :-/

Hoping for help with this and thanks in advance :-)

Answer 1

If you need the timestamp you can do:

var d = new Date(2016, 1, 1, 0, 0, 0, 0);
var timestamp = Math.floor(d / 1000); //this would be your timestamp

var d = new Date(2016, 1, 1, 0, 0, 0, 0); 
var timestamp = Math.floor(d / 1000); 
console.log(timestamp);

Answer 2

Just multiply your timestamp by 1000 to convert to ms and pass result directly to new Date()

You will need to print the timestamp, you can't mix language variables directly since your C# runs on server and javascript in the browser

var d= new Date(1483228800 * 1000) 
console.log(d)

Answer 3

You should try

Math.round(new Date().getTime()/1000)

It should return a timestamp in seconds. Also you can use the more accurate method:

new Date().valueOf()
READ ALSO
How to handle Quickblox session expiration?

How to handle Quickblox session expiration?

In quickblox, the session expires two hours after the last requestSo to handle this situation I have used the code

1137
Does an API store data that is extracted from an website permanently? [on hold]

Does an API store data that is extracted from an website permanently? [on hold]

I want to extract data from my university's server (like grades and attendance) and display it in an Android appThe problem with that server is that it is not always online

300
Using a config file for JWT secret in Node.js

Using a config file for JWT secret in Node.js

I am learning Nodejs, and while implementing JWT I want to put a separate config

484
Updating an array returned by a function

Updating an array returned by a function

I've been trying to implement some card game using JavascriptIn the snippet below, I simply want to pull two cards from the top of the deck and give it to the player (simplified logic below)

402