RSS

Tag Archives: date

How to Display Current Date in your program

Find it difficult to get the current date and time? Then read on, I this tutorial I demonstrate getting current date and time in different programming languages.

Java

Using Calendar

output: 28/6/2012

  Calendar cal = new GregorianCalendar();
  int month = cal.get(Calendar.MONTH);
  int year = cal.get(Calendar.YEAR);
  int day = cal.get(Calendar.DAY_OF_MONTH);
  System.out.println("Current date : " + day + "/" + (month + 1) + "/" + year);

Using SimpleDateFormat

output: 28/06/2012

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
String sDate= sdf.format(date);

C#
Using DateTime format

output: Jun Fri 27 11:41 2012

DateTime time = DateTime.Now;              // Use current time
string format = "MMM ddd d HH:mm yyyy";    // Use this format
Console.WriteLine(time.ToString(format));

Some other patterns for DateTime
MMM : display three-letter month
ddd : display three-letter day of the WEEK
d : display day of the MONTH
HH : display two-digit hours on 24-hour scale
mm : display two-digit minutes
yyyy : display four-digit year

Single-letter format

output :
6/28/2012
Thursday, June 28, 2012
Thursday, June 28, 2012 11:36 AM
Thursday, June 28, 2012 11:36:21 AM
6/28/2012 11:36 AM
6/28/2012 11:36:21 AM
June 28
June 28
2012-06-28T11:36:21.8863219+05:30
2012-06-28T11:36:21.8863219+05:30
2012-06-28T11:36:21
11:36 AM
11:36:21 AM
2012-06-28 11:36:21Z
Thursday, June 28, 2012 6:06:21 AM
June, 2012
June, 2012

DateTime now = DateTime.Now;
Console.WriteLine(now.ToString("d"));
Console.WriteLine(now.ToString("D"));
Console.WriteLine(now.ToString("f"));
Console.WriteLine(now.ToString("F"));
Console.WriteLine(now.ToString("g"));
Console.WriteLine(now.ToString("G"));
Console.WriteLine(now.ToString("m"));
Console.WriteLine(now.ToString("M"));
Console.WriteLine(now.ToString("o"));
Console.WriteLine(now.ToString("O"));
Console.WriteLine(now.ToString("s"));
Console.WriteLine(now.ToString("t"));
Console.WriteLine(now.ToString("T"));
Console.WriteLine(now.ToString("u"));
Console.WriteLine(now.ToString("U"));
Console.WriteLine(now.ToString("y"));
Console.WriteLine(now.ToString("Y"));

Using Date strings to control the output

output:
Thursday, June 28, 2012
12:03:53 PM
6/28/2012
12:03 PM
6/28/2012 12:03:53 PM

DateTime now = DateTime.Now;
Console.WriteLine(now.ToLongDateString());
Console.WriteLine(now.ToLongTimeString());
Console.WriteLine(now.ToShortDateString());
Console.WriteLine(now.ToShortTimeString());
Console.WriteLine(now.ToString());

PHP

output : 2012-06-28 17:33:07

$date = date('Y-m-d H:i:s');
echo $date;

output : 2012/06/28 17:33:07

$date = date('Y/m/d H:i:s');
echo $date;

This time is based on the default server time zone. To get the time in a different time zone it should be set first.

date_default_timezone_set('Australia/Sydney');
$date = date('Y/m/d H:i:s');
echo $date;

JavaScript

output : Thursday, June 28, 2012

<script type="text/javascript"><!--
var now = new Date();
var Weekday = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var Month = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
document.write(Weekday[now.getDay()]+", "+Month[now.getMonth()]+" "+now.getDate()+", "+now.getFullYear());
//--></script>

output : 6/28/2012

<script type="text/javascript"><!--
var now = new Date();
document.write((now.getMonth()+1)+"/"+now.getDate()+"/"+now.getFullYear());
//--></script>

output : 06/28/2012

<script type="text/javascript"><!--
var now = new Date();
var month = now.getMonth()+1;
if( month < 9 ) { month = "0"+month; }
var day = now.getDate();
if( day < 9 ) { day = "0"+day; }
document.write(month+"/"+day+"/"+now.getFullYear());
//--></script>

ios

 NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
 [dateFormatter setDateFormat:@"hh:mm a"];   
 NSString *str_date = [dateFormatter stringFromDate:[NSDate date]];
 NSLog(@"str_date:%@",str_date);
Advertisements
 
 

Tags:

Display date in different formats with PHP

echo date('Y-m-d H:i:s');

Sample output : 2012-05-20 11:33:44

 
Leave a comment

Posted by on May 20, 2012 in PHP

 

Tags: