views:

214

answers:

4

What is the vb.net or c# equivalent of the following javascript?

this.browserTime.value = Math.floor((new Date()).getTime() / 1000);

I'm using httpwebrequest to login in to a site.

PostData header recorded from the browser looks like:

goto=&currentSlave=235acbdcd297c9211eef670c6dfbd64d&browserTime=1245052940&username=username&password=password&go=Sign+In

and the javascript on the page that gets the browsertime value is:

this.browserTime.value = Math.floor((new Date()).getTime() / 1000);

thanks

+1  A: 

That depends on what exactly you need. You seem to want a timestamp. As such you can get it with

DateTime.Now

If you really desperately need the number of seconds since 1970, then you'd have to do some date math as the .NET timestamp isn't based on the UNIX epoch (which would be an implementation detail anyway).

Joey
Also worth mentioning DateTime.UtcNow
annakata
Shouldn't UtcNow be exactly the same timestamp, just with another time zone?
Joey
Sure, mentioned out of completeness, because local times on servers tends to be bad, and because the intent of the question seemed.... vague... to me :)
annakata
Hi,Not too sure what I need to use. I'm using httpwebrequest to login in to a site. the postdata from the post headers looks like the following:goto=
Bruce Adams
@Bryce - you should edit that into the post first, and then (if you're writing the handler to receive this?) add to it why browsertime is important. It sounds like you need the browser to give you a UTC time.
annakata
+1  A: 

number of seconds since 1970?

static readonly DateTime epoch = new DateTime(1970, 1, 1);
...
int seconds = (int)Math.Floor((DateTime.Now - epoch).TotalSeconds);
Marc Gravell
A: 

Just to add to Johannes comment, I'm guessing that you want to make a comparison between two dates. If so you can use the TimeSpace class like this:

DateTime now = DateTime.Now;
DateTime lastWeek = now.Subtract(TimeSpan.FromDays(7));
TimeSpan difference = now - lastWeek;
Ed Sykes
For info, you can also use now.AddDays(-7), which is simpler
Marc Gravell
Or use the subtraction operator: now - TimeSpan.FromDays(7). Or with MiscUtil extensions, now - 7.Days() which I think is the most obviously readable version :)
Jon Skeet
@Marc, yeah that way is simpler. I just forgot it when i wrote that example.@Jon - thanks for the heads up on MiscUtil, not seen that before, will check it out.
Ed Sykes
+1  A: 

Translation:

new Date() => DateTime.Now
.getTime() => .Subtract(New DateTime(1970, 1, 1)).TotalMilliseconds
Math.floor() => Math.Floor()

so in VB:

seconds As Double = Math.Floor( _
   DateTime.Now.Subtract(New DateTime(1970, 1, 1)).TotalMilliseconds / 1000
);

and in C#:

double seconds = Math.Floor(
   DateTime.Now.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds / 1000
);

or simply getting the seconds instead of getting milliseconds and dividing, in VB:

seconds As Double = Math.Floor(
   DateTime.Now.Subtract(New DateTime(1970, 1, 1)).TotalSeconds
);

and C#:

double seconds = Math.Floor(
    DateTime.Now.Subtract(New DateTime(1970, 1, 1)).TotalSeconds
);
Guffa