VBS objFile.DateLastModified and Date Format Settings - vbscript

I'm trying to do some date modify check for a file in my VBS script. But it looks like comparison operation depend on date format which set on machine where script is runnnig. I have diverse machines where Regioanal & date settings could be Russian, English US, English UK and I need to run my VBS against all machines and being able to compare date correctly.
When I'm trying to use
If objFile.DateLastModified = cdate("19.10.2012 11:34:06") then
do something
Else do something else
End IF
It seemingly works and doing correct comparison on machine with Russian formats setting, but fails on machine with English UK formats setting with following error
Type mismatch: 'cdate'
800A000D
If I use following DateSerial(2012,10,19) it doesn't throw an error but fail to compare dates correctly.
What is the best and easiest way to compare file modify date against predifined value with VBS irrespectively of machine date format setting?

Long story short: Don't use localized date formats when parsing dates.
ISO 8601 format works just fine:
If objFile.DateLastModified = CDate("2012-10-19 11:34:06") Then
' do something
Else
' do something else
End If
If you must, you can use the SetLocale() function explicitly to make your script run under different environments:
SetLocale 1049 ' LCID "Russian"
MsgBox CDate("5.4.2013 15:00:00") ' April 5, shown as 05.04.2013 15:00:00
SetLocale 1033 ' LCID "English - United States"
MsgBox CDate("5.4.2013 15:00:00") ' Type mismatch error
Refer to the list of assigned LCIDs on the MSDN.

Use
objFile.DateLastModified = DateSerial(2012,10,19) + TimeSerial(11,34,06)
Use a Date literal (format #mm/dd/yyyy h:mm:ss AM/PM#, locale-independent):
objFile.DateLastModified = #10/19/2012 11:34:06 AM#
Use what xkcd tells you.
objFile.DateLastModified = CDate("2012-10-19 11:34:06")

Related

How to convert Unix Time to (dd-MMM-yyyy) with FTL code

I am trying to convert a unix time to datetime depending on its locale given
Example I wanted to get locale Australia and convert UNIX time to date:
<#setting locale= getlocale>
<#assign unix = "1660545165"?number>
<#assign expiryDate = unix.getstarttime()?number?number_to_datetime?string["yyyy-mm-dd"]>
here is the time ${expiryDate}
I searched in google and suggested me to use getstarttime()?number?number_to_datetime?string["yyyy-mm-dd"]
However I am getting an error and currently stuck:
Expected a hash, but this has evaluated to a number
Lot of what's wrong I think is simply unnecessary there. Also based on the format you actually want a date, not a date-time. Here's a working example:
<#assign unixTime = 1660545165>
Here is the time: ${unixTime?number_to_date?string["yyyy-MM-dd"]}
Also usually you want to set the date_format setting to yyyy-MM-dd once (in the Java code, or with <#setting date_format = "yyyy-MM-dd"> near the top of the the template), and then just write ${unixTime?number_to_date} everywhere.

Convert date format, BMC Remedy/smart-it

Problem:
In a field called $Detailed Decription$ sometimes dateformat 08/09/2021 is enterd and this need to be converted to swedish format 2022-02-11
I'am going to use BMC Developer studio and make a filter but i cant find a fitting solution for it. Replacing it wont work (i think) becaus it need to have a value to replace it with.
Maby there can be a function that reads regex (\d{2})/(\d{1,2})/(\d{4}) but how can i convert it?
If it's sometimes - look at AR System User Preferencje form. Check certain user's locale and date time config.
Also is important where the data comes from. Could be a browser setting or java script mod.
1- Using Set fields action, copy the date value from Detailed Description to a Date/Time field (i.e. z1D_DateTime01).
2- Using Set fields action and Functions (MONTH, YEAR, DAY, HOUR, MINUTE, SECOND) you can parse the date/time and convert it to format you like.
Something like this:
SwedishDate = YEAR($z1D_DateTime01$) + "-" + MONTH($z1D_DateTime01$) + "-" + DAY($z1D_DateTime01$)
This will capture the parts of date and combine them with "-" in the middle and in the order you want.

How can I add days in a date like dd/mm/yyyy in powerapps?

I have a date in format dd/mm/yyyy and I have tried to add days to it.
My following code writes the label 18:
Text(DateValue(Label17.Text);"[$-pt-BR]dd/mm/yyyy";"pt-BR")
and this code add days(qtd_dias_para_contato) on label18:
DateAdd(DateValue(Label18.Text);Value(ThisItem.Qtde_dias_para_contato);Days)
On my computer the above code works but when I open it on my cellphone the data appears like mm/dd/yyyy. Why?
The DateAdd function returns a Date value in PowerApps, and when it needs to be converted to a text value (such as in a label), it will use the default formatting (for the system in which the app is running). If you want to force it to use a certain format, then you need to use the Text function:
Text(
DateAdd(
DateValue(Label18.Text);
Value(ThisItem.Qtde_dias_para_contato);
Days);
"dd/mm/yyyy")
If this expression is not working (following your comment), you can try "breaking it down" to make sure that all parts are working correctly. For example, try adding a label with the following Text property:
Year(DateValue(Label18.Text)) & " - " &
Month(DateValue(Label18.Text)) & " - " &
Day(DateValue(Label18.Text))
Is it showing the date that you expect (in this case, year - month - day)?
If this is working correctly, we can move on to the next step (Value(ThisItem.Qtde_dias_para_contato)); if you add a label (inside the gallery, I assume) with this Text property:
Value(ThisItem.Qtde_dias_para_contato)
Does it show the number that you expect? If so, we can move on to the next level (the DateAdd function) - if you add another label with the formula below for its Text property,
Year(DateAdd(DateValue(Label18.Text); Value(ThisItem.Qtde_dias_para_contato); Days)) & " - " &
Month(DateAdd(DateValue(Label18.Text); Value(ThisItem.Qtde_dias_para_contato); Days)) & " - " &
Day(DateAdd(DateValue(Label18.Text); Value(ThisItem.Qtde_dias_para_contato); Days))
Again, does it show the expected date?
Once we know exactly in which point of your expression the issue is happening, it will be easier to find a fix for it.

VBScript Convert value to Date [duplicate]

This question already has answers here:
Get dates from AUT?
(2 answers)
Closed 5 years ago.
I have a VBScript that runs on the developer machine, in which the following line of code
CDate("01/09/2017")
returns the date as 1 September 2017.
But when deployed on certain clients the same line of code returns 9 January 2017 as the date.
How can I control this?
This has been answered before in detail;
Use SetLocale() to choose how you want VBScript to interpret the value.
SetLocale(1106) 'Set to United Kingdom
WScript.Echo CDate("01/09/2017")
For valid Locale ID values see Microsoft Locale ID Values (you also appear to able to use valid IETF language tag codes as well like en-us etc).
Most likely the date string is parsed according to the regional settings of the respective system. For stable results across systems with different regional settings you probably need to parse the date yourself, e.g. like this:
s = "01/09/2017"
a = Split(s, "/")
d = DateSerial(a(2), a(1), a(0))

Classic ASP language (monthname for chinese)

Have been struggling to find a solution for this.
I am looking for monthname to return the chinese version of January (or spanish etc.). At the moment it is returning english.
The site is installed on an english windows machine.
I have tried adding session.lcid as well as: <%#LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
but I keep getting the english version of the month name (and to change each occurrence is a major code change). Also I need to try and keep the date format as per what I have as changing that might be an issue as well.
Can anyone recommend an answer? Much appreciated.
You need the SetLocale function and the locale identifier of the language.
Sample test vbs script
Option Explicit
Dim language, outputString
For Each language In Array( "es-es", "en-us", "zh" )
SetLocale language
outputString = outputString & MonthName( 2 ) & vbCrLf
Next
WScript.Echo outputString
Output

Resources