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
3 Answers
Reset to default 3You 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
评论列表(0条)