Since my development deals with the sysadmin domain, quite frequently I need to obtain the current date in my format of choice (yyyy-mm-dd).  This post is a reference post with examples of how to get the date in that format using VB script, C#, Python, bash, and dos batch.

VB Script:

   1: Dim strDayOfMonth: strDayOfMonth = right("0" & day(date()),2) 'Gives two-digit day of month.
   2: Dim strMonth: strMonth = right("0" & month(date()),2) 'Gives two-digit month.
   3: Dim strYear: strYear = year(date()) 'Gives four digit year.
   4: Dim strEightDigitDate: strEightDigitDate = strYear &"-"& strMonth &"-"& strDayOfMonth 'Concatenates date values to format YYYYMMDD.
   5: getEightDigitDate = strEightDigitDate 'Done.
   6: MsgBox(strEightDigitDate)

C#

   1: DateTime currentDate = DateTime.Parse(DateTime.Now.ToShortDateString());
   2: String yyyy_mm_dd = currentDate.ToString("yyyy-MM-dd");
   3: Console.WriteLine(yyyy_mm_dd);

 

Python

   1: #!/usr/local/bin/python
   2:  
   3: import datetime
   4: today = datetime.date.today()
   5: print today

 

Bash

   1: #!/bin/bash
   2:  
   3: today=`date +%Y-%m-%0e`
   4: echo $today
   5: exit 0

 

Dos Batch

   1: @ECHO OFF
   2: set year=%date:~10,4%
   3: set month=%date:~4,2%
   4: set day=%date:~7,2%
   5:  
   6: set yyyymmdd=%date:~10,4%-%date:~4,2%-%date:~7,2%
   7:  
   8: echo %year%
   9: echo %month%
  10: echo %day%
  11: echo %yyyymmdd%
posted on Tuesday, January 08, 2008 12:58 PM | Filed Under [ SysAdmin ]

Comments

Gravatar
# re: Date Time, vbscript, c#, python, bash, batch
Posted by Steve
on 2/26/2008 7:20 PM
hi - would you happen to still have the mfr specs?
Gravatar
# re: Date Time, vbscript, c#, python, bash, batch
Posted by Clint Rutkas
on 3/8/2008 2:38 AM
you can make the .Net stuff even smaller

Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd"));
Gravatar
# Huge boobs.
Posted by Boobs.
on 8/12/2008 5:40 PM
Boobs.
Post Comment
Title *
Name *
Email
Url
Comment *  
Please add 1 and 8 and type the answer here: