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

Mansa
  • 2,277
  • 10
  • 37
  • 67
  • Possible duplicate of [How do you get a timestamp in JavaScript?](http://stackoverflow.com/questions/221294/how-do-you-get-a-timestamp-in-javascript) – Mohit S Dec 02 '16 at 06:45
  • you can do `var time = new Date().getTime();` – bash0ne Dec 02 '16 at 06:49
  • in JavaScript Date.prototype.getTime() returns the number of milliseconds since 1 January 1970 – Jesús López Dec 02 '16 at 06: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? – Mansa Dec 02 '16 at 06: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. – Ralph Ritoch Dec 02 '16 at 07:40

3 Answers3

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

LenglBoy
  • 1,451
  • 1
  • 10
  • 24
  • I almost got it, but need the date to be the first day of this year (see my edited post) – Mansa Dec 02 '16 at 06:57
  • 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. – LenglBoy Dec 02 '16 at 07:13
  • Seriously? This answer isn't even correct. NO you can't use less parameters because the first parameter is the year. – Ralph Ritoch Dec 02 '16 at 07:16
  • @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 Dec 02 '16 at 07:19
  • LenglBoy, try it yourself... (new Date(2016,0,1)).getTime() it doesn't return the UTC for january 1st. – Ralph Ritoch Dec 02 '16 at 07:29
  • If this solution was correct it would return 1451606400000 which it doesn't (Unless you live in GMT+0) – Ralph Ritoch Dec 02 '16 at 07:37
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()); 
karel
  • 5,489
  • 46
  • 45
  • 50
Ralph Ritoch
  • 3,260
  • 27
  • 37
  • 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 Dec 02 '16 at 13:18
  • 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. – Ralph Ritoch Dec 04 '16 at 02:24
  • 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 Dec 04 '16 at 22:42
  • 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()); – Ralph Ritoch Dec 06 '16 at 04:16
  • Cool, was just suggesting something more concise but stopping short of obfuscation. ;-) – RobG Dec 06 '16 at 04:42
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!

Asif Ghanchi
  • 236
  • 3
  • 11