Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Conversion of javascript date to c# date

Posted on 2013-02-06
8
Medium Priority
?
409 Views
Last Modified: 2013-02-06
Hi

First off, I have zero experience with javascript, but I need to convert a datetime value in some html into a DateTime in c#.

In the html I see the following:

<script type="text/javascript">
    jQuery(document).ready(function() {
        jQuery('span.LocalTime').localTime('en');
    });
</script>

<td class="headerColored">
<span class='LocalTime' gmt_time='1360039849'></span>
</td>

That "gmt_time" is displaying in the browser as "Feb 05, 2013, 06:50AM"

My timezone is UTC+2

Can anybody help?
0
Comment
Question by:zattz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 

Author Comment

by:zattz
ID: 38858513
I have found the following function in the source javascript files:

/**
 * Calculates the User's local time using JS.
 *
 */
 
jQuery.fn.localTime = function() {
      this.self = this;
      self.selectedFormat = arguments[0];
      self.section = arguments[1];

      self.format = function() {
        if (self.section == 'billing') {
          switch(self.selectedFormat) {
              case 'en': return '%m/%d/%Y'; break;
              case 'es': return '%d de %n %Y'; break;
              case 'fr': return '%d %f %Y'; break;
              case 'pt': return '%d de %r %Y'; break;
              case 'de': return '%d. %g %Y'; break;
              case 'it': return '%d %t %Y'; break;
              case 'ja': return '%Y/%m/%d'; break;
              case 'zh': return '%Y/%m/%d'; break;
              case 'zh_TW': return '%Y/%m/%d'; break;
              default: return '<nobr>%d %b %Y</nobr>';
          }
        } else if (self.section == 'timeOnly') {
          switch (self.selectedFormat) {
              case 'en': return '%I:%M'; break;
              case 'es': return '%I:%M'; break;
              case 'fr': return '%I h %M'; break;
              case 'pt': return '%I:%M'; break;
              case 'de': return '%I.%M'; break;
              case 'it': return '%I:%M'; break;
              case 'ja': return '%I:%M'; break;
              case 'zh': return '%I:%M'; break;
              case 'zh_TW': return '%I:%M'; break;
              default: return '<nobr>%I:%M</nobr>';
          }
        } else if (self.section == 'board') {
            switch(self.selectedFormat) {
                case 'en': return '%b %e at %J:%M %p'; break;
                case 'es': return '%d de %n %Y %H:%M'; break;
                case 'fr': return 'le %e %f %Y %H h %M'; break;
                case 'pt': return '%e de %r %Y %H:%M'; break;
                case 'de': return '%e. %g %Y %H.%M'; break;
                case 'it': return '%e %t %Y, %H:%M'; break;
                case 'ja': return '%Y/%m/%d  %H:%M'; break;
                case 'zh': return '%Y/%m/%d  %H:%M'; break;
                case 'zh_TW': return '%Y/%m/%d  %H:%M'; break;
                default: return '<nobr>%d %b %Y</nobr> at <nobr>%H:%M</nobr>';
            }
        } else {      
            switch(self.selectedFormat) {
                case 'en': return '%b %d, %Y at %I:%M %p'; break;
                case 'es': return '%d de %n %Y %H:%M'; break;
                case 'fr': return 'le %d %f %Y %H h %M'; break;
                case 'pt': return '%d de %r %Y %H:%M'; break;
                case 'de': return '%d. %g %Y %H.%M'; break;
                case 'it': return '%d %t %Y, %H:%M'; break;
                case 'ja': return '%Y/%m/%d  %H:%M'; break;
                case 'zh': return '%Y/%m/%d  %H:%M'; break;
                case 'zh_TW': return '%Y/%m/%d  %H:%M'; break;
                default: return '<nobr>%d %b %Y</nobr> at <nobr>%H:%M</nobr>';
            }
        }
      };
     
      self.calculateTime = function(gmt) {
        return self.strftime(new Date(parseInt(gmt) * 1000));
      };

      self.strftime = function(theDate) {
        var fmt = self.format();

        for (var s in self.strftime_funcs)
          if (s.length == 1)
            fmt = fmt.replace('%' + s, self.strftime_funcs[s](theDate));
       
        return fmt;
      };
     
      self.strftime_funcs = {
        zeropad: function(n) { return n>9 ? n : '0'+n; },
        A:       function(t) { return ['Sunday','Monday','Tuedsay','Wednesday','Thursday','Friday','Saturday'][t.getDay()] }, //English Days
        F:       function(t) { return ['Dimanche','Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi'][t.getDay()] }, //French Days
        N:       function(t) { return ['Domingo','Lunes','Martes','Mi&eacute;rcoles','Jueves','Viernes','S&aacute;bado'][t.getDay()] }, //Spanish Days
        G:       function(t) { return ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'][t.getDay()] }, //German Days
        R:       function(t) { return ['Domingo','Segunda-feira','Ter&ccedil;a-feira','Quarta-feira','Quinta-feira','Sexta-feira','S&aacute;bado'][t.getDay()] }, //Portuguese Days
        T:       function(t) { return ['domenica','lunedì-feira','martedì','mercoledì','giovedì','venerdì','sabato'][t.getDay()] }, //Italian Days
       
        b:       function(t) { return ['January','February','March','April','May','June', 'July','August', 'September','October','November','December'][t.getMonth()] }, //English Months
        f:       function(t) { return ['janvier','février','mars','avril','mai','juin', 'juillet','août', 'septembre','octobre','novembre','décembre'][t.getMonth()] }, //French Months
        n:       function(t) { return ['enero','febrero','marzo','abril','mayo','junio', 'julio','agosto', 'septiembre','octubre','noviembre','diciembre'][t.getMonth()] }, //Spanish Months
        g:       function(t) { return ['Junuar','Februar','März','April','Mai','Juni', 'Juli','August', 'September','Oktober','November','Dezember'][t.getMonth()] }, //German Months
        r:       function(t) { return ['janeiro','fevereiro','março','abril','maio','junho', 'julho','agosto', 'setembro','outubro','novembro','dezembro'][t.getMonth()] }, //Portuguese Months
        t:       function(t) { return ['gennaio','febbraio','marzo','aprile','maggio','giugno', 'luglio','agosto', 'settembre','ottobre','novembre','dicembre'][t.getMonth()] }, //Italian Months
       
        c:       function(t) { return t.toString(); },
        d:       function(t) { return this.zeropad(t.getDate()); },
        e:       function(t) { return t.getDate(); },
        H:       function(t) { return this.zeropad(t.getHours()); },
        I:       function(t) { var h = t.getHours() % 12; if(h == 0){return 12;} return this.zeropad(h); },
        J:       function(t) { var h = t.getHours() % 12; if(h == 0){return 12;} return h; },
        m:       function(t) { return this.zeropad(t.getMonth()+1); }, // month-1
        M:       function(t) { return this.zeropad(t.getMinutes()); },
        p:       function(t) { return this.H(t) < 12 ? 'AM' : 'PM'; },
        S:       function(t) { return this.zeropad(t.getSeconds()); },
        w:       function(t) { return t.getDay(); }, // 0..6 == sun..sat
        y:       function(t) { return this.zeropad(this.Y(t) % 100); },
        Y:       function(t) { return t.getFullYear(); },
        Z:       function(t) {
                    if (t.getTimezoneOffset() > 0) {
                        return "-" + this.zeropad(t.getTimezoneOffset()/60) + "00";
                    } else {
                        return "+" + this.zeropad(Math.abs(t.getTimezoneOffset())/60) + "00";
                    }
                },    
        '%':     function(t) { return '%'; }
      };
     
      return this.each(function() {
          var runTrans = true;
          if (jQuery(this).attr('run_trans')) {
              runTrans = jQuery(this).attr('run_trans')=='yes'?true:false;
          }
          if (runTrans) {
              jQuery(this).html(self.calculateTime(jQuery(this).attr('gmt_time')));
          }
      });
};

jQuery.fn.localAge = function() {
    this.self = this;

    self.getDaysInMonth = function (month, year) {
          return 32 - new Date(year, month - 1, 32).getDate();
    };
   
    self.fixDate = function (day, month, year) {
          var daysInMonth;
         
        if (day <= 0) {            
              month--;
             
              if (month <= 0) {
                  month = 12 + month; // max month + possibly negative month
                  year--;
              }
             
              day = self.getDaysInMonth(month, year) + day; // days in month + possibly negative day
        } else if (month <= 0) {
            month = 12 + month; // max month + possibly negative month
            year--;
           
            daysInMonth = self.getDaysInMonth(month, year);
           
            if (day > daysInMonth)
                day = daysInMonth - (day - daysInMonth - 1);
        }
         
          return { day: day, month: month, year: year };
    };
   
    self.calculateAge = function (gmt) {
        return self.strfage(1000 * parseInt(gmt));
    };
   
    self.strfage = function (timestamp) {
          var today, day, month, year, threshold, newDate, dayOfWeek, startingYear, startingMonth;
       
        today = new Date();
        day = today.getDate();
        month = today.getMonth() + 1;
        year = today.getFullYear();
       
        threshold = new Date(year, month - 1, day).getTime();
       
        if (timestamp >= threshold)
            return mhDictionary.profile.today;
           
        day--;
       
        newDate = self.fixDate(day, month, year);
        day = newDate.day;
        month = newDate.month;
        year = newDate.year;
        threshold = new Date(year, month - 1, day).getTime();
           
        if (timestamp >= threshold)
            return mhDictionary.profile.yesterday;
           
        dayOfWeek = today.getDay();
        if (dayOfWeek > 0) {
            day -= dayOfWeek;
           
            newDate = self.fixDate(day, month, year);
            day = newDate.day;
            month = newDate.month;
            year = newDate.year;
            threshold = new Date(year, month - 1, day).getTime();
             
              if (timestamp >= threshold)
                  return mhDictionary.profile.thisWeek;
        }
       
        day -= 7;
       
        newDate = self.fixDate(day, month, year);
        day = newDate.day;
        month = newDate.month;
        year = newDate.year;
        threshold = new Date(year, month - 1, day).getTime();
           
        if (timestamp >= threshold)
            return mhDictionary.profile.lastWeek;
           
        if (month == (today.getMonth() + 1) && day > 1) { // last week isn't last month
              day = 1;
             
              threshold = new Date(year, month - 1, day).getTime();
                 
              if (timestamp >= threshold)
                  return mhDictionary.profile.overTwoWeeks;

              //if (timestamp >= threshold)
              //    return mhDictionary.profile.thisMonth;
        }
       
        month--;
       
        newDate = self.fixDate(day, month, year);
        day = newDate.day;
        month = newDate.month;
        year = newDate.year;
        threshold = new Date(year, month - 1, day).getTime();
           
        if (timestamp >= threshold)
              return mhDictionary.profile.overTwoWeeks;

        //if (timestamp >= threshold)
            //return mhDictionary.profile.lastMonth;
           
        startingMonth = month;
           
        for (month--; month > 0; month--) {
            newDate = self.fixDate(day, month, year);
            day = newDate.day;
            month = newDate.month;
            year = newDate.year;
            threshold = new Date(year, month - 1, day).getTime();
           
              if (timestamp >= threshold)
                  return mhDictionary.profile.overTwoWeeks;

              //if (timestamp >= threshold)
              //    return mhDictionary.profile.monthsAgo.replace('%MONTHS%', startingMonth - month + 2);
        }
       
        year--;
       
        newDate = self.fixDate(day, month, year);
        day = newDate.day;
        month = newDate.month;
        year = newDate.year;
        threshold = new Date(year, month - 1, day).getTime();
       
        if (timestamp >= threshold)
             return mhDictionary.profile.overTwoWeeks;

        //if (timestamp >= threshold)
        //    return mhDictionary.profile.lastYear;
           
        startingYear = year;
       
        do {
            year--;
           
            newDate = self.fixDate(day, month, year);
            day = newDate.day;
            month = newDate.month;
            year = newDate.year;
            threshold = new Date(year, month - 1, day);
        } while (timestamp < threshold); // let's hope it's not too long ago
           
        //return mhDictionary.profile.yearsAgo.replace('%YEARS%', startingYear - year + 1);
        return mhDictionary.profile.overTwoWeeks;
    };
   
    return this.each(function() {
        jQuery(this).html(self.calculateAge(jQuery(this).attr('gmt_time')));
    });
};
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38858655
sorry i might misunderstood you but why do u need to convert it to C# datetime in your client side?
do u pass the datetime to C# code server side, and that's where you need the conversion?
0
 

Author Comment

by:zattz
ID: 38858661
No. I have some raw html files I am saving to a database, and don't know how to convert the javascript date "1360039849" to "Feb 05, 2013, 06:50AM"
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 42

Expert Comment

by:sedgwick
ID: 38858776
here:

var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
var utcdate = d.toUTCString('1360039849');

Open in new window


from convert-utc-epoch-to-local-date-with-javascript
0
 

Author Comment

by:zattz
ID: 38858792
I need to convert the 1360039849 into a DateTime in c#
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 1500 total points
ID: 38858800
try this in your javascript code:
var d= new Date(); 
d = d.toUTCString(1360039849);
alert(d);

Open in new window

0
 

Author Comment

by:zattz
ID: 38858803
I got it.

var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(1360039849);

Thank you
0
 

Author Closing Comment

by:zattz
ID: 38858805
Thanks for the help
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question