Galactic Civilizations 3
Display the time and date in a batch script

Display the time and date in a batch script

Category: Programming.
Posted by on the 05/01/2014.
Last update on the 05/01/2014.

Description

This tutorial explains how to display and manipulate the time and date in a batch script on Windows.

Introduction

This tutorial is intended to people that uses Windows XP©, Windows Vista©, Windows 7© or Windows 8 and it does not require any prerequisites.
This tutorial can be realized with a simple text editor like « Notepad ».

Display time and date

To display the time and date in a batch script, you just need to use the following commands:

  • date : display the date;
  • time : display the time;

Here is a small example that shows how to use the commands:

@echo off
echo We are the %date%
echo And it is %time%

It will display:

We are the 25/02/2013
And it is 20:43:13.39

Split the date

By default, the date command displays the current date in the following format: jj/mm/aaaa.

To split the date, you need to use the following syntax:

%date:~startPosition,numberOfCharacters%

Here's how to split the date:

  • %date:~6,4% : display the year (Display 4 characters from position 6);
  • %date:~3,2% : display the month (Display 2 characters from position 3);
  • %date:~0,2% : display the day (Display 2 characters from position 0);

String

2

5

/

0

2

/

2

0

1

3

Positions

0

1

2

3

4

5

6

7

8

9

Here is an example that shows the splitting of the date:

echo The %date:~0,2% of %date:~3,2% %date:~6,4%

It will display:

The 25 of 02 2013

Split the time

By default, the time command displays the current time in the following format: hh:mm:ss.ms

To split the time, you need to use the following syntax:

%time:~startPosition,numberOfCharacters%

Here's how to split the time:

  • %time:~0,2% : display the hours (Display 2 characters from position 0);
  • %time:~3,2% : display the minutes (Display 2 characters from position 3);
  • %time:~6,2% : display the seconds (Display 2 characters from position 6);
  • %time:~9,2% : display the milliseconds (Display 2 characters from position 9);

String

2

0

:

4

4

:

4

1

. 0 0

Positions

0

1

2

3

4

5

6

7

8 9 10

Here is an example that shows the splitting of the time:

echo It is %time:~0,2% hours %time:~3,2% minutes %time:~6,2% seconds.

It will display:

It is 20 hours 44 minutes 41 seconds.

The date and time commands can be written in capitals "DATE", "TIME".

You like this tutorial ?
Like this website in order to promote it on facebook !

You like this tutorial ? Share it with your friends !
Partager sur Facebook Partager sur Twitter Partager sur Myspace Partager sur Stumbleupon Soumettre sur Reddit Partager sur Digg Ajouter à vos favoris Technorati Ajouter à vos favoris Live Ajouter à vos favoris Google Ajouter sur vos favoris Yahoo