How to convert a string value to date

Dear All,
I am new to powershell script, i was trying to store a Ad user password set date to a variable add, add a number of days to get the expire date.
but when i try to convert the variable to date value, I am getting the error as below.
Please help me......
PS C:\script> $passwordSetDate = (get-aduser user1 -properties * | select PasswordLastSet)
PS C:\script> $passwordSetDate
PasswordLastSet
7/15/2014 8:17:24 PM
PS C:\script> $a = [datetime]::ParseExact($passwordSetDate,"MM/dd/yyyy HH:MM:SS", $null)
Cannot find an overload for "ParseExact" and the argument count: "3".
At line:1 char:1
+ $a = [datetime]::ParseExact($passwordSetDate,"MM/dd/yyyy HH:MM:SS", $null)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
PS C:\script> $a = [datetime]::ParseExact($passwordSetDate,"MM/dd/yyyy HH:MM:SS", $null)

Dear All,
I am new to powershell script, i was trying to store a Ad user password set date to a variable add, add a number of days to get the expire date.
but when i try to convert the variable to date value, I am getting the error as below.
Please help me......
PS C:\script> $passwordSetDate = (get-aduser user1 -properties * | select PasswordLastSet)
PS C:\script> $passwordSetDate
PasswordLastSet
7/15/2014 8:17:24 PM
PS C:\script> $a = [datetime]::ParseExact($passwordSetDate,"MM/dd/yyyy HH:MM:SS", $null)
Cannot find an overload for "ParseExact" and the argument count: "3".
At line:1 char:1
+ $a = [datetime]::ParseExact($passwordSetDate,"MM/dd/yyyy HH:MM:SS", $null)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
PS C:\script> $a = [datetime]::ParseExact($passwordSetDate,"MM/dd/yyyy HH:MM:SS", $null)
In your post you ask how to convert a string value to a date.  The value returned from the Get-AdUser is already a date.  It does not need to be converted.
Bill has sshown one way to convert a date to a string and there are other methods.  You need to clarify your question.
If you are really trying ot convert strings to dates then you can start with this:
[datetime]'01/21/1965 13:33:23'
Most date strings aer autodetected by the class.
¯\_(ツ)_/¯

Similar Messages

  • How to convert a string into orcle date format

    i need some information about converting string into date format.i have string like '20-SEP-05' .so we have to convert into date formate like yy-mm-dd' (2005-09-20).how can we do it.

    Here's some code to help. I can't remember what method uses what format though
      public static java.sql.Date sqlDateValueOf( String dateString ) {
        String stringDate = null;
        if ( dateString == null || dateString.length() < 10 ) return null;
        String strDay = "";
        String strMonth = "";
        String strYear = "";
        if ( dateString.substring(4,5).equals( "-" ) ) {
          strDay = dateString.substring(8, 10);
          if ( strDay.length() < 2 ) strDay = "0" + strDay;
          strMonth = dateString.substring(5, 7);
          strMonth = dateString.valueOf(Integer.parseInt(strMonth) - 1);
          if ( strMonth.length() < 2 ) strMonth = "0" + strMonth;
          strYear = dateString.substring(0, 4);
          stringDate = strDay + "-" + strMonth + "-" + strYear;
        else if ( dateString.substring(2,3).equals( "-" ) ) {
          strDay = dateString.substring(0, 2);
          if ( strDay.length() < 2 ) strDay = "0" + strDay;
          strMonth = dateString.substring(3, 5);
          strMonth = String.valueOf(Integer.parseInt(strMonth) - 1);
          if ( strMonth.length() < 2 ) strMonth = "0" + strMonth;
          strYear = dateString.substring(6, 10);
          stringDate = strDay + "-" + strMonth + "-" + strYear;
        Calendar cal = Calendar.getInstance();
        cal.set( Calendar.YEAR, Integer.parseInt( strYear ) );
        cal.set( Calendar.MONTH, Integer.parseInt( strMonth ) );
        cal.set( Calendar.DAY_OF_MONTH, Integer.parseInt( strDay ) );
        java.sql.Date outDate = new java.sql.Date( cal.getTimeInMillis() );
        return outDate;
      public static String toDateString( java.util.Date date) {
        if ( date == null || date.toString().length() < 10 ) return null;
        String outDate = "";
        SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        outDate = df.format( date );
        return outDate;
      public static String toDateString( java.sql.Date date) {
        if ( date == null || date.toString().length() < 10 ) return null;
        String outDate = "";
        SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        outDate = df.format( date );
        return outDate;
      public static String toDateString( java.sql.Date date, String format) {
        if ( date == null || date.toString().length() < 10 ) return null;
        String outDate = "";
        SimpleDateFormat df = new SimpleDateFormat( format );
        outDate = df.format( date );
        return outDate;
      public static String toDateString( java.sql.Timestamp date) {
        if ( date == null || date.toString().length() < 10 ) return null;
        String outDate = "";
        SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        outDate = df.format( date );
        return outDate;
      }

  • Converting the string value to data format

    Hello Everyone,
                                  Please guide me in converting the value to date format.
    from source i'm getting the value (which is acchally a data value) '20070730'.
    I need this value to be in date format '2007/07/30'
    Please help me in getting it done.
    thank you

    Hi
    Code Snippetselect cast('20070730' as datetime)
    Note : beware of collation used in your SQL instance or your database.
    Jean-Pierre

  • How to convert a String into a date in yyyy-MM-dd using SimpleDateFormat?

    Hi Guys,
    I am using the following code
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    public class ValiDATE {
         private static boolean validateDateFormat(String strDate) {
              try {
                   SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd");
                   try {
                        formatter.setLenient(false);
                        formatter.parse(strDate);
                   catch (ParseException e) {
                        // invalid date/datetime format          
                        return false;
              catch (Exception ignored) {
              return true;
         public static void main(String args[]){
              System.out.println(validateDateFormat("11111-11-11"));          
    }In the above snippet even if I pass the string parameter as "11111-11-11" it returns a new date and hence prints TRUE in the console, but I want the date to be in yyyy-MM-dd format that is no of years shouldn't exceed 4 digits in all.
    I don't want to use RegEx, I know this can be done using SimpleDateFormat, but would like to know how that can be done.
    I think it has to do something with this . ( [http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html#year|http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html#year] )
    Regards
    AR

    Java.slogger wrote:
    Yeah I have gone through the javaDoc and it says For formatting, if the number of pattern letters is 2, the year is truncated to 2 digits; otherwise it is interpreted as a number. The important part is:
    JavaDoc:
    Any other numeric string, such as a one digit string, a three or more digit string, or a two digit string that isn't all digits (for example, "-1"), is interpreted literally.
    Do you know any workarounds else is using a Regex is the best approach?I don't see this as a problem. Everything behaves as designed and as is sensible.
    So if you want to avoid dates with 5-digit numbers, I'd add something like:
    if (parsedDate.after(CUTOFF_DATE)) {
      throw new OhNoesThisIsEvilException("!");
    }

  • How to convert multiple string values in a single row from nvarchar to int

    suppose I have a table like below.all these values are in nvarchar. How would I find the minimum year from START_DATE??
    PRODUCT_CODE 
    PRODUCT_DESC
    START_DATE
    P00002933
    VITAMINE C
    2005,2000,2011,2001,2002
    P00003087
    BIOFENAC
    2011,2009,2006,2007,2004,2005
    P00000155
    AMOXYPEN
    2006,2007,2008,2009,0000,2001
    P00002325
    SAHHA FARINE
    2008,2003,2002,2001,2009
    P00005666
    AMOXYPEN
    2007,2008,2006,2009,0000,2001

    Create the split function:
    CREATE FUNCTION Split (
    @InputString VARCHAR(8000),
    @Delimiter VARCHAR(50)
    RETURNS @Items TABLE (
    Item VARCHAR(8000)
    AS
    BEGIN
    IF @Delimiter = ' '
    BEGIN
    SET @Delimiter = ','
    SET @InputString = REPLACE(@InputString, ' ', @Delimiter)
    END
    IF (@Delimiter IS NULL OR @Delimiter = '')
    SET @Delimiter = ','
    --INSERT INTO @Items VALUES (@Delimiter) -- Diagnostic
    --INSERT INTO @Items VALUES (@InputString) -- Diagnostic
    DECLARE @Item VARCHAR(8000)
    DECLARE @ItemList VARCHAR(8000)
    DECLARE @DelimIndex INT
    SET @ItemList = @InputString
    SET @DelimIndex = CHARINDEX(@Delimiter, @ItemList, 0)
    WHILE (@DelimIndex != 0)
    BEGIN
    SET @Item = SUBSTRING(@ItemList, 0, @DelimIndex)
    INSERT INTO @Items VALUES (@Item)
    -- Set @ItemList = @ItemList minus one less item
    SET @ItemList = SUBSTRING(@ItemList, @DelimIndex+1, LEN(@ItemList)-@DelimIndex)
    SET @DelimIndex = CHARINDEX(@Delimiter, @ItemList, 0)
    END -- End WHILE
    IF @Item IS NOT NULL -- At least one delimiter was encountered in @InputString
    BEGIN
    SET @Item = @ItemList
    INSERT INTO @Items VALUES (@Item)
    END
    -- No delimiters were encountered in @InputString, so just return @InputString
    ELSE INSERT INTO @Items VALUES (@InputString)
    RETURN
    END -- End Function
    GO
    Use the below script:
    create table Table1 (Product_Code varchar(50),Product_Desc varchar(10),sStart_date varchar(MAX))
    Insert into Table1 Values('P00002933','VITAMINE C','2005,2000,2011,2001,2002'),
    ('P00003087','BIOFENAC','2011,2009,2006,2007,2004,2005')
    Select Product_Code,Product_Desc ,MIN(Item) Minstart_date
    From Table1 A
    Cross apply Split(A.sStart_date,',') B
    Group by Product_Code,Product_Desc
    Drop table Table1

  • How to convert a string to a Date obj ?

    Hi,
    String xx; "xx is from a table rs.getString();"
    Date dd = Date.valueOf(xx);
    id = today.compareTo(dd);
    Complier complains , why ?
    Thanks in advance.
    Philip

    Hi,
    I finally figured the reason. I have to put "java.sql.DatevalueOf( )" in the code. I guess there are 2 Date classes. One is in the Util.* and the other in sql.*...
    The one in Util.* means something else...
    Thanks anyway.
    Philip

  • How to convert a String("yyyy-mm-dd") to the same but in Date format ?

    Hi,
    can anyone plz tell me how to convert a String to a date format.I'm using MSACCESS database.I want to store this string in the database.So i need to convert it to a date format since the table is designed such a way with date/time type for date field.I used SimpleDateFormat ,but i can't able to convert.The code is given below:
    coding:
    public String dateconvertion(String strDate)
    try
    SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd");
    Date date = sdfSource.parse(strDate);
    SimpleDateFormat sdfDestination = new SimpleDateFormat("yyyy-MM-dd ");
    strDate = sdfDestination.format(date);
    catch(ParseException pe)
    System.out.println("Parse Exception : " + pe);
    return(strDate);
    }

    i used prepared statement even now i am getting error like this.....
    i have included the prepared statement package also...
    my coding:
    ResultSet rsdatetemp = null;
    PreparedStatement ps = null;
    String query ="select distinct itemcode from sales where bill date between ? and ?";
    ps = precon.prepareStatement(query);
    ps.setDate(1,d1);//d1 and d2 are in date format
    ps.setDate(2,d2);
    rsdatetemp = ps.executeQuery();
    error :
    symbol : method setDate(int,java.util.Date)
    location: interface java.sql.PreparedStatement
    ps.setDate(1,d1);
    symbol : method setDate(int,java.util.Date)
    location: interface java.sql.PreparedStatement
    ps.setDate(2,d2);

  • How do I know if I can convert a String value to an int value or not?

    Hi,
    I want to know how to make the judgment that if I can convert a String value to an int value or not? Assume that I don't know the String is number or letters
    Thank you

    Encephalopathic wrote
    Again, why?One of the problems (have been dued) in my codelab asks us to write a class as follow
    Write a class definition of a class named 'Value' with the following:
    a constructor accepting a single integer paramter
    a constructor with no parameters
    a method 'setVal' that accepts a single parameter,
    a boolean method, 'wasModified' that returns true if setVal was ever called for the object.
    a method 'getVal' that returns an integer value as follows: if setVal has ever been called, it getVal returns the last value passed to setVal. Otherwise if the "single int parameter" constructor was used to create the object, getVal returns the value passed to that constructor. Otherwise getVal returns 0.
    The setVal(int y) returns nothing, so how do I know whether it has been called or not?
    Thank you

  • How to convert a string to a currency value ? getting a dump with exception

    Hi,
    I am getting a exception not caught in CX_SY_CONVERSION_NO_NUMBER.
    I am trying to convert a string value with ',' to a currency value.
    I tried using Replace and Condense. But the error still persists.
    Is there a FM or a casting that I can use?
    Cheers
    Kiran

    Hi,
    Sorry I got my question wrong. I have a problem - that when I'm trying to pass a value from a string to a currency field.
    But, the currency field is a field symbol.
    so, I have
    data abc type string.
    abc = "5345"
    <curr_val> = abc.
    now <curr_val> = 0.000
    Please suggest.

  • How to convert ResultSet's value to String

    Dear JDC's
    plz tell me how to convert ResultSet's value to String.
    kashif

    I want to read a whole array from a table into a String[]
    and then send it to another servlet before I forward it all to anther servlet with request.setParameter.....
    Any ideas how to solve it....that is how far I got so far:
    rs=pgresult("select count (answer) from answers where qid="+idnum);
                                                           numberofquestions = rs.getInt("mynumber");     
                                                           rs=pgresult("SELECT answer FROM answers WHERE qgroup="+qgroup);
                                                           allanswers = new String[numberofquestions];
                                                           allanswers = (String[])rs.getArray(1).getArray();     
                                                           req.setParameter("allanswers",allanswers);
                                                           RequestDispatcher dispatcher = req.getRequestDispatcher("editanswers");
                                                           dispatcher.forward(req,res);
    Tobi

  • How to convert a string

    I have a string "March 2006", that I would like to convert into two dates, one is first date of the month and second is the last date of the month.
    For example:
    String strDate="March 2006"
    Now I want to convert it into two dates:
    03/01/2006 and 03/31/2006 and assign those two dates to two variables.
    I have been trying something like this just to convert string to date but no luck:
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         java.util.Date mydate = new java.util.Date();
         try{
         mydate = sdf.parse("March 2006");
         }catch(ParseException e){}
         System.out.println("Java data object is"+mydate);
    I get following output : Tue Jun 20 10:34:30 CDT 2006

    Thanks for your patience guys. First I am not a java
    expert and just a new bee to Java, and second I guess
    we are going into a different direction here. My
    basic problem is that is there a way to convert
    "March 2006" string to 03/2006? Then once we get
    03/2006, can we find what is the first date ,
    offcourse 01, and what is the last date, which in
    this case is 31?Convert the String to a Date, as you've already been shown how to do
    Use the java.util.Calendar methods to get your last day of the month value (check the javadoc, especially the get method and look at the fields) Post back with any specific questions.
    Good Luck
    Lee
    >
    For example:
    My variable is "March 2006". The end product I want
    is:
    strDate1=03/01/2006
    strDate2=03/31/2006.
    How should I get my end product?
    I can write string manipulation functions to get this
    done,but trying to see if there is an easiest way to
    do this. Thanks.

  • How to convert a String to an Icon

    Hello,
    I'm trying to figure out how to convert a String (specifically one letter) to an Icon for a JTree. The program I am working on uses .png files for the icons at the moment, with each .png being a single uppercase letter. However, recent discussion has us wanting to change to something more dynamic, since we anticipate needing new letters in the near future.
    My task is to modify the current codebase so that the Icons used to identify nodes in the tree are created from a Font within code, rather than reading in .png files. I have done some experimenting with creating a Label with just that String, setting the font and trying to extract the image using calls like createImage(int width, int height) and so on, but so far to no avail.
    If I could figure out how to at least convert the String to an image, I could probably make it work (since I can play around with creating an ImageIcon with that image). Thanks in advance for any help.
    Allan

    Here is some quick and dirty code:
    import java.awt.*;
    import javax.swing.*;
    public class BasicIcon implements Icon {
        private String text;
        public BasicIcon(String text) {
            this.text = text;
        public int getIconWidth() {
            return 16;
        public int getIconHeight() {
            return 16;
        public void paintIcon(Component c, Graphics g,  int x, int y) {
            Color old = g.getColor();
            g.setColor(c.getForeground());
            g.drawString(text, x, y+13);
            g.setColor(old);
        public static void main(String[] args) {
            JPanel cp = new JPanel();
            for (char ch = 'A'; ch < 'F'; ++ch) {
                cp.add(new JButton("letter " + ch, new BasicIcon(String.valueOf(ch))));
            JFrame f = new JFrame("BasicIcon");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setContentPane(cp);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
    }One thing that could be improved is the vertical positioning of the letter in the Icon.
    The x,y values passed into paintIcon are the upper-left hand corner of the icon,
    but the y value needed by drawString is the position of the String's baseline.
    To do it right, use one of Font's getLineMetrics methods and play around with ascent.

  • How to get a string "yyyyMMdd" into date like MM/dd/yyyy?.

    Hi
    How to get a string "yyyyMMdd" into date like MM/dd/yyyy?. i have used
    SimpleDateFormat.parse but it is giving long value. i want to disply the date
    like MM/dd/yyyy
    thank you

    Do you want the string rearranged or do you want to create an instance of some form of Date class?
    If you want to rearrange the string you could just do it yourself provided you know the date string is going to be of the format yyyyMMdd.
    something like this:
    String newDate = origStr.charAt(4) + origStr.charAt(5) + "/" + origStr.charAt(6) + origStr.charAt(7) + "/" ...etc.

  • How to capture the string values

    Hi Gurus,
    Could you plz suggest me how to capture the string value.
    My requirement is.
    I have a structure field called  cnj_stat-sttxt_int and it is storing a value like "REL CDRT AVAC". AND the moddile value CDRT will change according to condtions. now i required to write a logic like if cnj_stat-sttxt_int field contain 'REL' and 'AVAC'.  I HAVE TO THROW SOME ERROR.
    SO please give some idea about on it...
    Its urgent..
    points will be rewarded...
    Thanks in advance!!!!
    Thanks & regards,
    Kranthi.

    if cnj_stat-sttxt_int field contain 'REL' and 'AVAC'. I HAVE TO THROW SOME ERROR.
    if cnj_stat-sttxt_int CS 'REL' and cnj_stat-sttxt_int CS 'AVAC'.
    THROW SOME ERROR.
    endif.

  • How to convert a string from upper case to lower case in FOX formula

    Hi Experts,
    How to convert a string from upper case to lower case in FOX formula?
    Thanks,
    Cheers!!!
    PANKAJ

    The last result.append( c ) should be:
    result.append( Character.toLowerCase(c) );

Maybe you are looking for