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%