2

I'm using the following code to pass to my javascript code

sb.Append("start: new Date(" + Convert.ToDateTime(appointment.AppointmentDate).Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds.ToString() + "),");

The problem is that it's not including the time along with the date. How can I include the time as well?

Thanks!

1

5 Answers 5

1

You can parse date/time string which is in en-US format.

Put this code in your aspx page into the SCRIPT tag and you will have your DateTime value in dt variable.

var dtString = '<%= MyDateTimeObject.ToString((new System.Globalization.CultureInfo("en-US")).DateTimeFormat) %>';

var dt = new Date(dtString );
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is probably regional settings, try:

sb.Append("start: new Date(" + Convert.ToDateTime(appointment.AppointmentDate).Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat) + "),");

4 Comments

No luck. I'm getting dates like this: new Date(1288260000000)
Se my answer below, new Date(<number you got>) The number is the milliseconds. The above should work by the way, try pasting this in your url bar: javascript:alert(new Date(1288260000000) )
@Mike are you sure your AppointmentDate is correct? All the examples given and the one you gave yourself should have at least some kind of time component in it.
You're right. I am getting the time component with it. I tested it in the firebug console. I guess the javascript isn't translating it correctly on the client side. At least I know the problem isn't on the server side. Thanks!
0

If you have .NET 3.5+ or the AJAX Extensions for .NET 2.0, you may already have the methods you need to convert objects to JavaScript in the JavaScriptSerializer class.

Add a reference to System.Web.Extensions.

using System.Web.Script.Serialization;

...

var jsSerializer = new JavaScriptSerializer();
string jsDate = jsSerializer.Serialize(appointment.AppointmentDate);

Now, on the JS side, you're going to have to undo the JSON serialization using a JSON parser.

Comments

0

One way is to use Date.parse that can handle many more formats.

var d = new Date(Date.parse("<your date here>"));

You need to use new Date and Date.parse because Date.parse does not return a date object but number of milliseconds since 1970 nnn something.

Comments

0
using System;

class Program
{
    static void Main(string[] args)
    {
        DateTime? myTime = new DateTime(1970, 1, 1, 0, 0, 1);

        Console.Write(myTime.Value.ToUnixTimeInMs().ToString());
        Console.Read();
    }
}

public static class DateTimeExtensions
{
    private static readonly DateTime unixEpoch = new DateTime(1970, 1, 1);

    public static double ToUnixTimeInMs(this DateTime dateTime)
    {
        return dateTime.Subtract(DateTimeExtensions.unixEpoch).TotalMilliseconds;
    }
}

Simply pass the return value of ToUnixTimeInMs() into the constructor of a JavaScript Date object.

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.