Jump to content

Converting time and date


Recommended Posts

I would like to turn the string 20.11.2008 at 03:53:06, Which is in Germany time (GMT+1) and convert it to 2 variables, $Date containing 11/20/2008 and $Time containing 11:53:06 PM EST (GMT+5).

Here is a long-winded way to do it.

#include <date.au3>

$datetime0 = "20.11.2008 at 03:53:06"

;split into date, at, time
$datetime1 = stringsplit($datetime0," ")

;get just the date
$date0 = $datetime1[1]

;split into day,month, year
$date1 = stringsplit($date0,".")

;rebuild into YYYY/MM/DD
$date = $date1[3] & '/' & $date1[2] & '/' & $date1[1]

;addthe time back
$datetime = $date & ' ' & $datetime1[3]

;now we have the datetime in the correct format so we can add adjust
;(read the date.au3 include file)
$datePlus5 = _DateAdd("h",8,$datetime)
ConsoleWrite("Datetime + 8 hours = " & $dateplus5 & @CRLF)
$Result = stringsplit($datePlus5,' ')
ConsoleWrite("Time + 8 hours is " & $result[2] & @CRLF)
$date2 = stringsplit($Result[1],'/')
$ResDate = $date2[2] & '/' & $date2[3] & '/' & $date2[1]
ConsoleWrite("Date + 8 hours in your format is " & $resdate & @CRLF)
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

And here's a somewhat shorter-winded way :mellow: :

#include <date.au3>

$datetime0 = "20.11.2008 at 03:53:06"

;rebuild date & time into YYYY/MM/DD & time for use with _DateAdd
$datetime = StringRegExpReplace($datetime0,"(\d+)\.(\d+)\.(\d{4})\sat\s((\d{2}):(\d{2}):(\d{2}))","\3/\2/\1 \4")

;now we have the datetime in the correct format so we can add adjust
;(read the date.au3 include file)
$datePlus5 = _DateAdd("h",8,$datetime)
ConsoleWrite("Datetime + 8 hours = " & $dateplus5 & @CRLF)

$time = StringRegExpReplace($datePlus5,"(\d{4}/\d+/\d+)(?:\s)(\d+\:\d+\:\d+)","\2")
$ResDate = StringRegExpReplace($datePlus5,"(\d{4}/\d+/\d+)(?:\s)(\d+\:\d+\:\d+)","\1")
ConsoleWrite("Time + 8 hours is " & $time & @CRLF)

ConsoleWrite("Date + 8 hours in your format is " & $resdate & @CRLF)
Link to comment
Share on other sites

And here's a somewhat shorter-winded way :mellow: :

#include <date.au3>

$datetime0 = "20.11.2008 at 03:53:06"

;rebuild date & time into YYYY/MM/DD & time for use with _DateAdd
$datetime = StringRegExpReplace($datetime0,"(\d+)\.(\d+)\.(\d{4})\sat\s((\d{2}):(\d{2}):(\d{2}))","\3/\2/\1 \4")

;now we have the datetime in the correct format so we can add adjust
;(read the date.au3 include file)
$datePlus5 = _DateAdd("h",8,$datetime)
ConsoleWrite("Datetime + 8 hours = " & $dateplus5 & @CRLF)

$time = StringRegExpReplace($datePlus5,"(\d{4}/\d+/\d+)(?:\s)(\d+\:\d+\:\d+)","\2")
$ResDate = StringRegExpReplace($datePlus5,"(\d{4}/\d+/\d+)(?:\s)(\d+\:\d+\:\d+)","\1")
ConsoleWrite("Time + 8 hours is " & $time & @CRLF)

ConsoleWrite("Date + 8 hours in your format is " & $resdate & @CRLF)
That's a bit better ResNullius! There was no way I could have come up with that at 5.00 in the morning and probably not any other time :(.

Now I've worked out what you did, would it be safer to allow for more than one space each side of the at? (Which my attempt hadn't allowed for either.)

$datetime = StringRegExpReplace($datetime0,"(\d+)\.(\d+)\.(\d{4})\s+at\s+((\d{2}):(\d{2}):(\d{2}))","\3/\2/\1 \4")
Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
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...