Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Wednesday, 7 February 2018

Simple Java program to print current date and time.


Java program to display current date.

Date function in Java Programming code.



/*
Java Date example.
This Java Date example program describes how Java Date function from utility is being used in Java language.
*/

import java.util.*;

public class JavaDateProgram{

          public static void main(String args[]){
            /*
              Create date object with current date and time.
            */
 Date dateandtime = new Date();
  System.out.println("Current Time is " + dateandtime);

  }
}

Output:


Current Time is Sat Feb 08 16:10:21 IST 2018

Sunday, 2 October 2016

Javascript Date


The Javascript Date object lets you work with dates (years, months, days, hours, minutes, seconds, and milliseconds)

Simply for displaying dates its like below:
<script>
document.getElementById("demo").innerHTML = Date();
</script>

The important methods of date object are as follows:

MethodDescription
getFullYear()returns the year in 4 digit e.g. 2015. It is a new method and suggested than getYear() which is now deprecated.
getMonth()returns the month in 2 digit from 1 to 31.
getDate()returns the date in 1 or 2 digit from 1 to 31.
getDay()returns the day of week in 1 digit from 0 to 6.
getHours()returns all the elements having the given name value.
getMinutes()returns all the elements having the given class name.
getSeconds()returns all the elements having the given class name.
getMilliseconds()returns all the elements having the given tag name.

Current date example:
<script> 
var today=new Date();
document.getElementById('txt').innerHTML=today;
</script>

Current Time example:
<script> 
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
</script>