1

I want to show current datetime in my webpage which will show current counter for datetime. Means it will increase time for each second like c# timer.

I used following code for this.

    <label id = "TimerValue" ></label>
    var test = new Date($.now());
    var timer = $.timer(function () {
     var ttt = moment(++test).format(Tag);
      $("#TimerValue").html(ttt);
     });
   timer.set({ time: 1000, autostart: true });

this code increases value. But Format is "1419227503752";

This value increasing Each Seconds.

May be this value is Correct but now i want to change this value to my defined Datetime formats.

Some of the formats are as below:

yyyy-MM-dd HH:mm:ss
yyyy-MM-dd HH:mm
yyyy/MM/dd HH:mm:ss
yyyy/MM/dd HH:mm
yyyy年MM月dd日 HH時mm分ss秒
yyyy年MM月dd日 HH時mm分
yyyy年MM月dd日
HH時mm分ss秒
HH:mm:ss
HH時mm分
HH:mm
HH時

timer i have using plug in Where moment is plugin for formatting date time String.

I also refer this but no Success.

Can anybody help me out this?

Community
  • 1
  • 1
Jankya
  • 966
  • 2
  • 12
  • 34

1 Answers1

1

Maybe this can help you (getting each value and create a string to show the time with the format you are creating):

function updateClock ( )
    {
    var currentTime = new Date ( );
    var currentHours = currentTime.getHours ( );
    var currentMinutes = currentTime.getMinutes ( );
    var currentSeconds = currentTime.getSeconds ( );

    // Pad the minutes and seconds with leading zeros, if required
    currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
    currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

    // Choose either "AM" or "PM" as appropriate
    var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

    // Convert the hours component to 12-hour format if needed
    currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

    // Convert an hours component of "0" to "12"
    currentHours = ( currentHours == 0 ) ? 12 : currentHours;

    // Compose the string for display
    var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;


    $("#clock").html(currentTimeString);

 }

$(document).ready(function()
{
   setInterval('updateClock()', 1000);
});

http://www.sitepoint.com/create-jquery-digital-clock-jquery4u/

kleinohad
  • 5,800
  • 2
  • 26
  • 34