CodesJava

Easy learning with example program codes

TypeScript date object tutorial


TypeScript date object:

A TypeScript Date object represents a date. We can get or set year, month and day, hour, minute, second, and millisecond fields of the object by Date methods.

TypeScript Date Object Properties:

Property Description
constructor It specifies the function that creates an object’s prototype.
prototype It allows us to add properties and methods to an object

TypeScript Date Object Methods:

Method Description
Date() It returns today’s date and time
getDate() It returns the day of the month for the specified date according to local time.
getDay() It returns the day of the week for the specified date according to local time.
getFullYear() It returns the year of the specified date according to local time.
getHours() It returns the hour in the specified date according to local time.
getMilliseconds() It returns the milliseconds in the specified date according to local time.
getMinutes() It returns the minutes in the specified date according to local time.
getMonth() It returns the month in the specified date according to local time.
getSeconds() It returns the seconds in the specified date according to local time.
getTime() It returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC.
getTimezoneOffset() It returns the time-zone offset in minutes for the current locale.
getUTCDate() It returns the day (date) of the month in the specified date according to universal time.
getUTCDay() It returns the day of the week in the specified date according to universal time.
getUTCFullYear() It returns the year in the specified date according to universal time.
getUTCHours() It returns the hours in the specified date according to universal time.
getUTCMilliseconds() It returns the milliseconds in the specified date according to universal time.
getUTCMinutes() It returns the minutes in the specified date according to universal time.
getUTCMonth() It returns the month in the specified date according to universal time.
getUTCSeconds() It returns the seconds in the specified date according to universal time.
getYear() It returns the year in the specified date according to local time. Use getFullYear instead.
setDate() It sets the day of the month for a specified date according to local time.
setFullYear() It sets the full year for a specified date according to local time.
setHours() It sets the hours for a specified date according to local time.
setMilliseconds() It sets the milliseconds for a specified date according to local time.
setMinutes() It sets the minutes for a specified date according to local time.
setMonth() It sets the month for a specified date according to local time.
setSeconds() It sets the seconds for a specified date according to local time.
setTime() It sets the Date object to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC.
setUTCDate() It sets the day of the month for a specified date according to universal time.
setUTCFullYear() It sets the full year for a specified date according to universal time.
setUTCHours() It sets the hour for a specified date according to universal time.
setUTCMilliseconds() It sets the milliseconds for a specified date according to universal time.
setUTCMinutes() It sets the minutes for a specified date according to universal time.
setUTCMonth() It sets the month for a specified date according to universal time.
setUTCSeconds() It sets the seconds for a specified date according to universal time.
setYear() It sets the year for a specified date according to local time. Use setFullYear instead.
toDateString() It returns the “date” portion of the Date as a human-readable string.
toGMTString() It converts a date to a string, using the Internet GMT conventions. Use toUTCString instead.
toLocaleDateString() It returns the “date” portion of the Date as a string, using the current locale’s conventions.
toLocaleFormat() It converts a date to a string, using a format string.
toLocaleString() It converts a date to a string, using the current locale’s conventions.
toLocaleTimeString() It returns the “time” portion of the Date as a string, using the current locale’s conventions.
toSource() It returns a string representing the source for an equivalent Date object; you can use this value to create a new object.
toString() It returns a string representing the specified Date object.
toTimeString() It returns the “time” portion of the Date as a human-readable string.
toUTCString() It converts a date to a string, using the universal time convention.
valueOf() It returns the primitive value of a Date object.

TypeScript Date Object Methods:

Method Description
Date.parse() Parses a string representation of a date and time and returns the internal millisecond representation of that date.
Date.UTC() It returns the millisecond representation of the specified UTC date and time.

How to create TypeScript date object:

  • new Date()
  • new Date(milliseconds)
  • new Date(datestring)
  • new Date(year,month,date[,hour,minute,second,millisecond ])

Example:

function dateTest(date:Date):void{
  var hours=date.getHours();
  var minutes=date.getMinutes();
  var seconds=date.getSeconds();
  console.log("Current Time: "+hours+":"+minutes+":"+seconds); 
}
var date=new Date();
dateTest(date);

Try it:

JS Bin on jsbin.com

TypeScript Tutorial

TypeScript Overview.
Typescript vs Javascript.
Typescript let vs var.
TypeScript Hello World.
Typescript Data Types.
Typescript Variable Declaration.
TypeScript operators list.
TypeScript Arithmetic Operators.
TypeScript Comparison Operators.
TypeScript Bitwise Operators.
Typescript Logical Operators.
TypeScript Assignment Operators.
Typescript Conditional Operator.
Typescript String Operator.
Typescript typeof Operator.
TypeScript control statements.
TypeScript Switch Statement.
TypeScript for loop.
TypeScript for in loop.
TypeScript while loop.
TypeScript do while loop.
Typescript for loop break.
TypeScript for loop continue.
Typescript function.
Typescript returning function.
Typescript parameterized function.
Typescript function optional parameter.
Typescript rest parameters.
Typescript default parameters.
Typescript anonymous function.
Typescript function constructor.
Typescript Arrow/Lambda function.
Typescript function overloading.
TypeScript number object.
TypeScript string object.
TypeScript boolean object.
TypeScript date object.
TypeScript math object.
TypeScript array object.
TypeScript tuples.
TypeScript union type.
TypeScript interface.
TypeScript interface inheritance.
TypeScript interface multiple inheritance.
TypeScript class.
Typescript class inheritance.
Typescript class interface.
TypeScript abstract class.
TypeScript object.
TypeScript duck typing.
TypeScript NameSpace.
TypeScript Multi-file namespaces.
TypeScript module.
TypeScript ambient module.


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap