Dynamic Date Populate In TypeScript

Introduction

 
The Date object is used to work with dates and times. Date objects are created with "new Date()". This is the date and time on the user's computer, not on the webserver.
 
In this article, we select a date format, then the TextBox will show the current date in that format.
 
The following example dynamically shows the formatted time using a web application in TypeScript. In this example, we use four radio buttons and a TextBox in our Form. Give all the radio buttons the same name. We make four functions in this example. The first function "NormalShow()" shows the normal date. In NormalShow(), we use the getDate(), getMonth() and getYear() functions to get the current Date, Month and Year. The second function is "ShortDateShow()". Here we use the tolocaleDateString() method to return the data portion as a string depending on the Locale. The third function is "LongDateShow()". Here the toLocaleString() method is used to convert the date to a string depending on the Locale. The fourth function is "UTCDateTimeShow()". Here ToUTCString() converts the date to a string, depending on the Coordinated Universal Time (UTC).
 
Step 1
Open Visual Studio 2012 and click "File" -> "New" -> "Project...". A window is opened. In this window, click "HTML Application for TypeScript" under Visual C#.
 
Give of your application the name "Window_History" and then click "Ok".
 
Step 2
After this session the project has been created; a new window is opened on the right side. This window is called the Solution Explorer. The Solution Explorer contains the ts file, js file and CSS files and the aspx page.
 
Coding
 
app.ts
  1. class DynamicTimeFormat {  
  2.  NormalShow() {  
  3.   var a = new Date();  
  4.   ( < HTMLTextAreaElement > document.getElementById('Display')).value = a.getDate() + "/" + a.getMonth() + "/" + a.getFullYear();  
  5.  }  
  6.  ShortDateShow() {  
  7.   var a = new Date();  
  8.   ( < HTMLTextAreaElement > document.getElementById('Display')).value = a.toLocaleDateString();  
  9.  }  
  10.  LongDateShow() {  
  11.   var a = new Date();  
  12.   ( < HTMLTextAreaElement > document.getElementById('Display')).value = a.toLocaleString();  
  13.  }  
  14.  UTCDateTimeShow() {  
  15.   var a = new Date();  
  16.   ( < HTMLTextAreaElement > document.getElementById('Display')).value = a.toUTCString();  
  17.  }  
  18. }  
  19. window.onload = () => {  
  20.  var obj = new DynamicTimeFormat();  
  21.  var objnormal = document.getElementById("rdonormal");  
  22.  var objshort = document.getElementById("rdoshort");  
  23.  var objlong = document.getElementById("rdolong");  
  24.  var objutc = document.getElementById("rdoutc");  
  25.  objnormal.onclick = function() {  
  26.   obj.NormalShow();  
  27.  }  
  28.  objshort.onclick = function() {  
  29.   obj.ShortDateShow();  
  30.  }  
  31.  objlong.onclick = function() {  
  32.   obj.LongDateShow();  
  33.  }  
  34.  objutc.onclick = function() {  
  35.   obj.UTCDateTimeShow();  
  36.  }  
  37. };  
Dynamic_time_format_demo.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Dynamic_time-format_demo.aspx.cs" Inherits="Dynamic_Time_Format_Populate.Dynamic_time_format_demo" %>  
  2.  <  
  3.  !DOCTYPE html >  
  4.  <  
  5.  html xmlns = "http://www.w3.org/1999/xhtml" >  
  6.  <  
  7.  head runat = "server" >  
  8.  <  
  9.  title > < /title>  
  10.  <  
  11.  script src = "app.js" > < /script>  
  12.  <  
  13.  style type = "text/css" > 
  14.  #Radio1 {  
  15.   width: 24 px;  
  16.  } 
  17. # Radio2 {  
  18.  width: 24 px;  
  19. }  
  20. #  
  21. Radio3 {  
  22.  width: 24 px;  
  23. }  
  24. #  
  25. Radio4 {  
  26.  width: 24 px;  
  27. }  
  28. #  
  29. Text1 {  
  30.  width: 245 px;  
  31.  height: 25 px;  
  32. }  
  33. #  
  34. Display {  
  35.  width: 372 px;  
  36. }  
  37. <  
  38. /style>  
  39. <  
  40. /head>  
  41. <  
  42. body >  
  43.  <  
  44.  form id = "form1"  
  45. runat = "server" >  
  46.  <  
  47.  div >  
  48.  <  
  49.  asp: Label ID = "Label1"  
  50. runat = "server"  
  51. Font - Bold = "True"  
  52. Font - Italic = "True"  
  53. Font - Size = "Large"  
  54. ForeColor = "#006600"  
  55. Text = "Dynamic Time Format Populate in TypeScript" > < /asp:Label>    
  56.  <  
  57.  /div>  
  58.  <  
  59.  /form>  
  60.  <  
  61.  p >  
  62.  <  
  63.  input id = "rdonormal"  
  64. name = "R1"  
  65. type = "radio"  
  66. value = "Normal Date" / > Normal Date < /p>  
  67.  <  
  68.  p >  
  69.  <  
  70.  input id = "rdoshort"  
  71. name = "R1"  
  72. type = "radio"  
  73. value = "Short Date" / > Short Date < /p>  
  74.  <  
  75.  p >  
  76.  <  
  77.  input id = "rdolong"  
  78. name = "R1"  
  79. type = "radio"  
  80. value = "Long Date" / > Long Date < /p>  
  81.  <  
  82.  p >  
  83.  <  
  84.  input id = "rdoutc"  
  85. name = "R1"  
  86. type = "radio"  
  87. value = "UTCDateTime" / > UTC Date / Time < /p>  
  88.  <  
  89.  p >  
  90.  <  
  91.  input id = "Display"  
  92. type = "text"  
  93. readonly = "true" / > < /p>  
  94.  <  
  95.  /body>  
  96.  <  
  97.  /html>  
app.js
  1. var DynamicTimeFormat = (function() {  
  2.  function DynamicTimeFormat() {}  
  3.  DynamicTimeFormat.prototype.Show = function() {  
  4.   var a = new Date();  
  5.   (document.getElementById('Display')).value = a.getDate() + "/" + a.getMonth() + 1 + "/" + a.getFullYear();  
  6.  };  
  7.  DynamicTimeFormat.prototype.ShowShortDate = function() {  
  8.   var a = new Date();  
  9.   (document.getElementById('Display')).value = a.toLocaleDateString();  
  10.  };  
  11.  DynamicTimeFormat.prototype.ShowLongDate = function() {  
  12.   var a = new Date();  
  13.   (document.getElementById('Display')).value = a.toLocaleString();  
  14.  };  
  15.  DynamicTimeFormat.prototype.ShowUTCDateTime = function() {  
  16.   var a = new Date();  
  17.   (document.getElementById('Display')).value = a.toUTCString();  
  18.  };  
  19.  return DynamicTimeFormat;  
  20. })();  
  21. window.onload = function() {  
  22.  var obj = new DynamicTimeFormat();  
  23.  var objnormal = document.getElementById("rdonormal");  
  24.  var objshort = document.getElementById("rdoshort");  
  25.  var objlong = document.getElementById("rdolong");  
  26.  var objutc = document.getElementById("rdoutc");  
  27.  objnormal.onclick = function() {  
  28.   obj.Show();  
  29.  };  
  30.  objshort.onclick = function() {  
  31.   obj.ShowShortDate();  
  32.  };  
  33.  objlong.onclick = function() {  
  34.   obj.ShowLongDate();  
  35.  };  
  36.  objutc.onclick = function() {  
  37.   obj.ShowUTCDateTime();  
  38.  };  
  39. };  
Output  
 
Animation1.gif
 
Referenced By
http://www.typescriptlang.org/


Similar Articles