0

I have following code to get current uct time and time from json API and see the output it shows in comments section of the code.I want to display both the times in the following form (DD/MM/YYYY HH:MM AM/PM) for example 15/08/2013 10.43 AM .Please let me know which plugin to use and also code changes needed.

<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="Scripts/json2.js" type="text/javascript"></script>
    <script src="Scripts/json.js" type="text/javascript"></script>

 <script type="text/javascript" language="javascript">

        $(document).ready(function () {

 //current UTC Time displays output as 15/8/2013 10:43:51 not showing AM or PM

            var currentutctime = new Date();
            var formatdate = currentutctime.getUTCDate() + '/' + (currentutctime.getUTCMonth() + 1) + '/' + currentutctime.getUTCFullYear() + ' ' + currentutctime.getUTCHours() + ':' + currentutctime.getUTCMinutes() + ':' + currentutctime.getUTCSeconds();
            $('#lblutctime').text(formatdate)

 //JSON API Time displays output as August 15,2013 10:42:59 GMT+0100 , not showing AM or PM

            $.ajax({
                url: 'http://timeapi.org/utc/now.json',
                dataType: 'jsonp'
            })

         .done(function (response) {
           $('#lblntp').text(response.dateString);
         })

       })

    </script>
    </head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblutctime" runat="server" Text="Label"></asp:Label>
        <asp:Label ID="lblntp" runat="server" Text="Label"></asp:Label>
     </div>
    </form>
</body>
</html>
user2580179
  • 259
  • 6
  • 15

2 Answers2

1

Use moment.js http://momentjs.com/ library for date formatting

Amith
  • 1,424
  • 1
  • 10
  • 22
0

jQuery is about interacting with page elements. It won't help you with time parsing/formatting, you would need another library.

Note that timeapi allows you to get output directly in your preferred format, so there is no need to parse and re-format:

http://www.timeapi.org/utc/now?\m/\d/\Y+\H:\M+\p

However please note that timeapi's timezones are wrong: they are calculated relative to local time on the server it runs on, without correct DST handling. So at the moment it is reporting the time to be 2013-08-15T12:53:21+01:00, which at +1:00 is obviously not UTC. I suggest using a less broken service. Ideally one that returns you plain-old-POSIX-timestamp as a number so you can just pass it to new Date(timestamp*1000) without having to worry about timezones or parsing.

bobince
  • 528,062
  • 107
  • 651
  • 834
  • Can you please let me know about plain-old-POSIX-timestamp ?Where or How should I get it. – user2580179 Aug 15 '13 at 12:56
  • A Unix (POSIX) timestamp is just the number of seconds that have elapsed since the epoch (the moment of 1970-01-01 00:00:00 UTC). Because it measures absolute time you don't have to worry about timezones or calendars, so this kind of numerical storage format is usually the most convenient to handle in your applications (until it comes to the point you have to present it to a user, in which case you may have to localise it depending on their timezone and locale preferences). – bobince Aug 15 '13 at 20:47
  • You can create a JS timestamp from a Unix time with the simple `new Date(n*1000)` constructor; the `*1000` is because JS works in milliseconds rather than seconds. Yahoo had a web service that would serve you with time in a variety of formats, including Unix time and direct JS time in milliseconds. They have deprecated it now though. Usually what you do is use your own server script for time; then you can fetch it with having to worry about cross-origin scripting issues. – bobince Aug 15 '13 at 20:49
  • @bobince “you can fetch it with having to worry about cross-origin scripting issues” → you probably meant ‘without’. – Marcel Korpel Aug 20 '13 at 19:04