Jump to content

How To Get Year-Month-Day From Variable


Recommended Posts

I am extracting these dates from .XML file and would like to now the best way to Extract YYYY-MM-DD from 2007-11-16T15:50:09.000Z

so the new $date variable would look like this

2007-11-16

Instead of the way with time

Published: 2007-11-16T15:50:09.000Z

Updated: 2008-11-12T01:11:45.000Z

Link to comment
Share on other sites

simple solution

$Split = StringSplit("2007-11-16T15:50:09.000Z",":")
$Split = StringSplit($Split[1],"T")
MsgBox("","",$Split[1])
Edited by komalo
[font="Palatino Linotype"][size="3"]AutoIt Script Examples :[/size][/font][font="Palatino Linotype"][size="3"]_CaptureBehindWindowGlass CMD for Windows Vista/Seven[/size][/font][left][/left][font="Palatino Linotype"][size="3"]Non AutoIt Script programs : Border Skin - Aero Glass On XP[/size][/font]
Link to comment
Share on other sites

Why split twice...

$String = "2007-11-16T15:50:09.000Z"
$Split = StringSplit($String,"T")
MsgBox("","",$Split[1])

8)

well , i tried Splitting using ":" First

and forget to remove it

Edited by komalo
[font="Palatino Linotype"][size="3"]AutoIt Script Examples :[/size][/font][font="Palatino Linotype"][size="3"]_CaptureBehindWindowGlass CMD for Windows Vista/Seven[/size][/font][left][/left][font="Palatino Linotype"][size="3"]Non AutoIt Script programs : Border Skin - Aero Glass On XP[/size][/font]
Link to comment
Share on other sites

thanks it work perfect.. and i used the single line instead of 2 lines :mellow: thanks again

$published = StringSplit($published,"T")
Remembering that StringSplit() returns an array when you use make use of $published... :(

Also, there is a policy around here that there must be at least one answer that includes a RegExp (SmOke_N is responsible for policing this policy):

$String = "2007-11-16T15:50:09.000Z"
$avSplit = StringRegExp($String,"(\d+-\d+-\d+)T(\d+:\d+:\d+.\d+)Z", 3)
If IsArray($avSplit) Then
    MsgBox(64, "Date/Time", $avSplit[0] & " " & $avSplit[1])
Else
    MsgBox(16, "Error", "Malformed line:  " & $String)
EndIf

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Remembering that StringSplit() returns an array when you use make use of $published... :mellow:

Also, there is a policy around here that there must be at least one answer that includes a RegExp (SmOke_N is responsible for policing this policy):

$String = "2007-11-16T15:50:09.000Z"
$avSplit = StringRegExp($String,"(\d+-\d+-\d+)T(\d+:\d+:\d+.\d+)Z", 3)
If IsArray($avSplit) Then
    MsgBox(64, "Date/Time", $avSplit[0] & " " & $avSplit[1])
Else
    MsgBox(16, "Error", "Malformed line:  " & $String)
EndIf

:(

Before the cop jumps you for the RegExp here is a revised version

;
$String = "2007-11-16T15:50:09.000Z"

$avSplit = StringRegExp($String,"(\d{4}-\d{2}-\d{2})T", 1)
If IsArray($avSplit) Then
    MsgBox(64, "Date", $avSplit[0])
Else
    MsgBox(16, "Error", "Malformed line:  " & $String)
EndIf


;; To return YYYY  MM  DD as separate elements use the following which
;; will actually get strings that are given as
;; $String = "2007-11-16T15:50:09.000Z"
;; $String = "2007-11-16T"
;; $String = "2007-11-16"
;; $String = "20071116"
$avSplit = StringRegExp($String,"(\d{4})-?(\d{2})-?(\d{2})T?|\z", 3)

If IsArray($avSplit) Then
    MsgBox(64, "Date", "Year: " & $avSplit[0] & @CRLF & "Month: " & _
    $avSplit[1] & @CRLF & "Month Day: " & $avSplit[2])
Else
    MsgBox(16, "Error", "Malformed line:  " & $String)
EndIf

;; To get the time from the same example you would use
$String = "2007-11-16T15:50:09.000Z"
$avSplit = StringRegExp($String,".*T(\d{2}:\d{2}:\d{2})\d?Z?|\z", 1)
If IsArray($avSplit) Then
    MsgBox(64, "Date", $avSplit[0])
Else
    MsgBox(16, "Error", "Malformed line:  " & $String)
EndIf
;

Now you won't be alone when he reads these. He can jump both of us. Of course he won't do that, he'll just add another one into the mix.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...