locked
parameter in Powershell RRS feed

  • Question

  • I was instructed to change datestamp  from yyyymmdd to mmddyyy in followinf code how can I do that:

    [datetime]$beginDt = $inBeginDt;

    [string]$beginDtYYYYMMDD = $beginDt.ToString("yyyyMMdd");

    [datetime]$endDt = $inEndDt;

    [string]$endDtYYYYMMDD = $endDt.ToString("yyyyMMdd");

    and also I have oracle query  that has following statement.

    ((ms.ymdeff <=  $beginDtYYYYMMDD and ms.ymdend >= $beginDtYYYYMMDD ) OR (ms.ymdeff  between $beginDtYYYYMMDD and $endDtYYYYMMDD))) 

    I am new to this and like to know how to execute this.

    Thank you very much

    so far I have tried  following but  when I use mmddyyy I am not getting   my query executed but when I use yyyymmdd it works fine 

    [datetime]$beginDt = $inBeginDt;
    [string]$beginDtMMDDYYYY = $beginDt.ToString("yyyyMMdd");
    [datetime]$endDt = $inEndDt;
    [string]$endDtMMDDYYYY = $endDt.ToString("yyyymMdd");

    "beginDtMMDDYYYY = " + $beginDtMMDDYYYY;
    "endDtMMDDYYYY   = " + $endDtMMDDYYYY;#>

    [bool]$dataError = $false;


    Vijay Patel


    • Edited by Vijay Patel Thursday, November 7, 2013 8:43 PM
    • Moved by Bill_Stewart Tuesday, March 25, 2014 3:09 PM Abandoned
    Thursday, November 7, 2013 6:49 PM

Answers

  • [string]$endDtMMddyyyy = $endDt.ToString("MMddyyyy");

    Or in other words: You need to modify the value used in the 'ToString' method which tells PowerShell what format to convert the datetime object to.

    • Proposed as answer by jrv Friday, November 8, 2013 8:13 AM
    • Marked as answer by Just Karl Wednesday, April 15, 2015 9:20 PM
    Friday, November 8, 2013 7:32 AM

All replies

  • [string]$endDtMMddyyyy = $endDt.ToString("MMddyyyy");

    Or in other words: You need to modify the value used in the 'ToString' method which tells PowerShell what format to convert the datetime object to.

    • Proposed as answer by jrv Friday, November 8, 2013 8:13 AM
    • Marked as answer by Just Karl Wednesday, April 15, 2015 9:20 PM
    Friday, November 8, 2013 7:32 AM
  • Be careful. The format string is case sensitive. MM is two day month. mm is minutes.

    PS C:\scripts> [datetime]::Now.ToString('MM')
    11
    PS C:\scripts> [datetime]::Now.ToString('mm')
    15
    PS C:\scripts> [datetime]::Now.ToString('M')
    November 8
    PS C:\scripts> [datetime]::Now.ToString('MMM')
    Nov
    PS C:\scripts> [datetime]::Now.ToString('MMMM')
    November
    PS C:\scripts>


    ¯\_(ツ)_/¯

    Friday, November 8, 2013 8:15 AM