c# - Create timestamp in javascript - Stack Overflow

I am trying to do this:string thisReturn = "";DateTime now = DateTime.UtcNow;now = new DateT

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

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

Share Improve this question edited Dec 2, 2016 at 6:55 Mansa asked Dec 2, 2016 at 6:41 MansaMansa 2,32511 gold badges39 silver badges69 bronze badges 5
  • Possible duplicate of How do you get a timestamp in JavaScript? – Mohit S Commented Dec 2, 2016 at 6:45
  • you can do var time = new Date().getTime(); – bash0ne Commented Dec 2, 2016 at 6:49
  • in JavaScript Date.prototype.getTime() returns the number of milliseconds since 1 January 1970 – Jesús López 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? – Mansa 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 patible with your C# code. – Ralph Ritoch Commented Dec 2, 2016 at 7:40
Add a ment  | 

3 Answers 3

Reset to default 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()

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

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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745455490a4628466.html

相关推荐

  • c# - Create timestamp in javascript - Stack Overflow

    I am trying to do this:string thisReturn = "";DateTime now = DateTime.UtcNow;now = new DateT

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信