c# function equivalent to javascript gettime function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anj2k5
    New Member
    • Sep 2007
    • 6

    c# function equivalent to javascript gettime function

    Hello ,

    I want to create a function in C#.net which is equivalent to gettime(millise conds since midnight 1st January, 1970 ) function in javascript.

    Actually i need to calculate unique value in c#.net as given in following javascript
    Code:
    //javascript
    var unique = d.getTime() + '' + Math.floor(1000 * Math.random());

    I used following code in c#

    Code:
    string val2;
    Random ran=new Random();
    int number=ran.Next(0,1000);
    val2 = (int) Math.Floor(number);
    But I dnt which function is used for gettime.


    Pls.help me..


    Anjana
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    .NET has the DateTime object.
    Use DateTime.Now to get the current Date and Time.

    Comment

    • anj2k5
      New Member
      • Sep 2007
      • 6

      #3
      But it gives time in different form,I want to actual format of gettime()

      Comment

      • Shashi Sadasivan
        Recognized Expert Top Contributor
        • Aug 2007
        • 1435

        #4
        Originally posted by anj2k5
        But it gives time in different form,I want to actual format of gettime()
        use the ToString() method to return your specific format.
        cheers

        Comment

        • anj2k5
          New Member
          • Sep 2007
          • 6

          #5
          But i dnt knw which format i have to provide in tostring() which is equivalent to
          gettime() in javascript.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Originally posted by anj2k5
            But i dnt knw which format i have to provide in tostring() which is equivalent to
            gettime() in javascript.
            Well how about LOOKING at it, and deciding.
            OR look up to see what format gettime() in javascript returns as and then you can force the DateTime object to output in that format.

            Time to do some of your own leg work there.

            Comment

            • anj2k5
              New Member
              • Sep 2007
              • 6

              #7
              gettime function of javascript return milliseconds since midnight 1st January, 1970.
              Would you tell me for this which format i should used?

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Originally posted by anj2k5
                gettime function of javascript return milliseconds since midnight 1st January, 1970.
                Would you tell me for this which format i should used?
                Well to ensure it:

                Code:
                private Int64 GetTime()
                {
                   Int64 retval=0;
                   DateTime st=DateTime(1970,1,1);
                   TimeSpan t= (DateTime.Now-st);
                   retval= (Int64)(t.TotalMilliseconds+0.5)
                   return retval;
                }
                There, did the whole thing for you.

                Comment

                • anj2k5
                  New Member
                  • Sep 2007
                  • 6

                  #9
                  Thanks for this.I'll apply this

                  Comment

                  • paotech
                    New Member
                    • Jan 2009
                    • 1

                    #10
                    Thanks!

                    Thanks for writing up this description. I'm not sure that the original poster had a full appreciation for your comments and support but I do - it saved me a great amount of time. Problem solved!

                    Originally posted by Plater
                    Well to ensure it:

                    Code:
                    private Int64 GetTime()
                    {
                       Int64 retval=0;
                       DateTime st=DateTime(1970,1,1);
                       TimeSpan t= (DateTime.Now-st);
                       retval= (Int64)(t.TotalMilliseconds+0.5)
                       return retval;
                    }
                    There, did the whole thing for you.

                    Comment

                    • MCBoy
                      New Member
                      • Feb 2010
                      • 3

                      #11
                      the corrected one!

                      UTC should be taken into consideration!!

                      Code:
                      private Int64 GetTime()
                      {
                         Int64 retval=0;
                         DateTime st=DateTime(1970,1,1);
                         TimeSpan t= (DateTime.Now.ToUniversalTime()-st);
                         retval= (Int64)(t.TotalMilliseconds+0.5)
                         return retval;
                      }

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        Maybe, but javascript doesn't take UTC into effect.
                        The idea was to mimic javascript.
                        Your function could be called:
                        private Int64 GetUTCTime()

                        Comment

                        • MCBoy
                          New Member
                          • Feb 2010
                          • 3

                          #13
                          it is UTC guys

                          by default javascript date object is UTC. All dates are calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC) with a day containing 86,400,000 milliseconds.

                          refer: http://www.w3schools.c om/js/js_obj_date.asp (W3Schools, JavaScript Date Object).

                          Comment

                          Working...