2

I am trying to do this:

string thisReturn = "";

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

Which will return this: 1451606400000


EDIT

I have now done this:

var unix = Math.round(+new Date()/1000);
var timestamp = unix+"000";

This will return this: 1480661530000

Almost there, but how do I set it to the 1st of january current year?


Now, how do I do exactly the same in javascript?

Hoping for help and thanks in advance :-)

5
  • Possible duplicate of How do you get a timestamp in JavaScript? Commented Dec 2, 2016 at 6:45
  • you can do var time = new Date().getTime(); Commented Dec 2, 2016 at 6:49
  • in JavaScript Date.prototype.getTime() returns the number of milliseconds since 1 January 1970 Commented Dec 2, 2016 at 6:49
  • OK, I almost got it (read edited) but i need it to set the timestamp for the beginning of the year. How do I do that? Commented Dec 2, 2016 at 6:56
  • Mansa the answer you selected is not correct. I strongly encourage you to look at the output it produces since it isn't in UTC and therefore isn't compatible with your C# code. Commented Dec 2, 2016 at 7:40

3 Answers 3

3

You can just do the same in javascript

    var d = new Date();
    var n = d.getTime();

Ofcause you can add some parameters for new Date()

Sign up to request clarification or add additional context in comments.

6 Comments

I almost got it, but need the date to be the first day of this year (see my edited post)
so you can use this: new Date(year, month, day, hours, minutes, seconds, milliseconds) of cause with less parameters if there is no special time needed.
Seriously? This answer isn't even correct. NO you can't use less parameters because the first parameter is the year.
@RalphRitoch you can just use new Date(year, month, day) there is no time needed. time will be 0:00:00 o´clock then
LenglBoy, try it yourself... (new Date(2016,0,1)).getTime() it doesn't return the UTC for january 1st.
|
1

The following will get you the timestamp for the beginning of the year.  

thisReturn = (function(d) { 
    d.setUTCMilliseconds(0);
    d.setUTCSeconds(0);
    d.setUTCMinutes(0);
    d.setUTCHours(0);
    d.setUTCMonth(0);
    d.setUTCDate(1); 
  return + d; })(new Date()); 

5 Comments

That's a very longwinded way of doing Date.UTC(2016,0,1). If a UNIX time value is required, then Date.UTC(2016,0,1)/1000.
RobG, there's no need for the /1000 if you note their code they add the zero's at the end. Either way, you still need to grab the current UTC year for your code to work. This longwinded method only requires the creation and destruction of one date object so it should be more efficient.
At least you could do d.setUTCHours(0,0,0,0); d.setUTCMonth(0,1) to remove 4 function calls. I don't think calls to new Date are that expensive.
Robg don't forget every object you create also needs to be destructed. Regardless I profiled it and using Date.UTC with a second date object on Firefox is about 3X faster than the method I came up with. Regardless this was about being correct I wasn't presenting this as the most optimized solution. My solution was the only correct solution (yours would be also if you submitted it). I coded yours as thisReturn = (function(d) { var d2 = Date.UTC(d.getFullYear(),0,1); return + d2; };)(new Date());
Cool, was just suggesting something more concise but stopping short of obfuscation. ;-)
0

To get the timestamp in seconds, you can use:

Math.floor(Date.now() / 1000)

Or alternatively you could use:

Date.now() / 1000 | 0

Please let me know if its worked for you.

Thanks!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.