VBScript Convert value to Date [duplicate] - vbscript

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))

Related

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.

ZEN : Allow multiple date formats in a dateText control and converting them to the YYYY-MM-DD

There is a finite list of date formats that users want to use to enter a date in a form. These formats include single digits for month and day and double digits for year. The field is represented by a dateText control.
How would one get to allow a dateText control to accept multiple date formats ? I see only 3 listed (https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=GZCP_forms_dateText), do those include using single digits for month and day ?
I tried to set the value of format = "#(myPageProperty.myValue)# " but I got a compilation error in Studio so that went nowhere. Has anyone ever been able to set the format value depending on the user input value?
I am guessing that the control input value must be converted to the YYYY-MM-DD before validation. I am open to calling a javascript function to do that but where would be the best place to put it?
for details see Class %ZEN.Component.dateText
setting format:
Property format As %ZEN.Datatype.string(MAXLEN = 3, **VALUELIST = ",MDY,DMY,YMD",** ZENEXPRESSION = 1)
you have exactly 3 formats or ""
Your guess on values is correct and documented:
/// The value of this control is always in the canonical form: YYYY-MM-DD
As this is one of the oldest components of ZEN your only chance to achieve
your way of operation is to create your own version inheriting from
Class %ZEN.Component.dateText and overloading the parts you want to change

VS Reporting Services - [rsInvalidFormatString] The Format value for the textrun is not valid

I wrote code in SQL Server that gives the year, the month and some data for those months in separate columns. The column for month (MM) gives months in numeric digits (1 to 12).
The code I used was:
Select ....
,Month(IRDate) AS MM
,...
I created a report in MS VS Report builder and included all the data in table columns. The report runs fine. However, at the bottom I get the below warning message:
[rsInvalidFormatString] The Format value for the textrun
‘MM.Paragraphs[0].TextRuns[0]’ is not valid. Format specifier was
invalid.
I checked the formatting for that cell, it is set to default - No formatting.
In the report, I've set the parameter to have a default value as
=DateValue("September 01, 2016")
Would that be the issue? Maybe the default value is not the right format? I removed the default value and kept it as no specified value. Warning message still comes up.
How can I fix this?
Have you tried formatting the textbox properties that holds the MM value to be a number datatype with no decimal places?
Regarding your other question about the default parameter value, I have typically seen DateValue formatted as such: 9/1/2016
Also, is your parameter datatype set to Date/Time?
Apparently, the error message '[rsInvalidFormatString] The Format value for the textrun....' is a log message. Rectifying the problem does not take away the message because it is a log message. So even after you correct the issue, the message will still show.
The only way to remove that message is to simply restart Visual Studio. When Visual Studio starts back up, the log is cleared and you won't see the message any more.
"Who knew healthcare would be so complicated?" ;)

kernel32.GetDateFormatEx and ar-SA dates

I am using GetDateFormatEx (more specifically, ctypes.windll.kernel32.GetDateFormatEx using Python), which is documented at
https://msdn.microsoft.com/en-us/library/windows/desktop/dd318088%28v=vs.85%29.aspx
And using today's date as the SYSTEMTIME input, and a NULL format string and 0 flags. however specifically for the ar-SA locale.
It is returning a value of "06/01/37", which is not what I expected. I expected to be in the ar-SA language (ie, what, in my English eyes, look like squibbles).
I also expected it would return an arabic short date string, specifically, for the gregorian calendar, however it seems to be using the Hijri calendar instead. How do I detect that a Hijri calendar is being used?
So, to re-iterate, what I want to know is:
How do I get the arabic date, something more like: تشرين الأول أكتوبر ١٩ ١٥
How do I detect the type of calendar that the user is expecting to be using?
Is it possible that you yourself has overridden the locale settings for date formatting on your testing PC? Maybe you need to use the LOCALE_NOUSEROVERRIDE flag.
UPDATE
I have tested this in C++
GetDateFormatEx(L"ar-SA", LOCALE_NOUSEROVERRIDE | DATE_AUTOLAYOUT | DATE_LONGDATE, nullptr, nullptr, buf, _countof(buf), nullptr);
The resulting date is
‏06/‏محرم/‏1437
UPDATE 2
This apparently depends on how Windows define the locale. I have tried different combination of the flags (DATE_AUTOLAYOUT | DATE_LONGDATE, DATE_AUTOLAYOUT | DATE_USE_ALT_CALENDAR | DATE_LONGDATE, DATE_AUTOLAYOUT | DATE_USE_ALT_CALENDAR). While for "ar-SA" this gives
‏06/‏محرم/‏1437
‏06/‏01/‏1437
‏06/‏01/‏37
for "ja-JP" (Japanese) it gives
2015‎年‎10‎月‎19‎日
平成 ‎27‎年‎10‎月‎19‎日
平成 ‎27/‎10/‎19
I suspect (not knowing full Windows NLS capabilities), you will have to use either specific formatting string or some other library, like ICU, to get more squigly dates.
UPDATE 3
It probably does not get any more squigly than with DATE_AUTOLAYOUT | DATE_USE_ALT_CALENDAR flags and L"dddd, dd MMMM, yyyy gg" format:
‏الإثنين, ‏06 ‏محرم, ‏1437 ‏بعد الهجرة
UPDATE 4
Further search reveals this:
Note: If you got the Gregorian date with Arabic names, then you probably forgot to set the calendar type to the Hijri calendar.
So it seems your Windows would have to have the Hijri calendar selected in your locale for the numbers to get formatted with the traditional numerals.
UPDATE 5
Finally, how do I detect that the Hijri calendar is being used in that case?
You can detect the calendar that is in use by calling GetLocaleInfoEx() with LOCALE_ICALENDARTYPE as its LCType parameter and comparing the returned number with constants defined in WinNls.h:
#define CAL_HIJRI 6 // Hijri (Arabic Lunar) calendar
#define CAL_UMALQURA 23 // UmAlQura Hijri (Arabic Lunar) calendar

Is there programmatical way to get short day names in windows?

Is there a way to get a 2 character day-name of the week such as MO/TU/WE/TH/FR/SA/SU?
Currently I only know of using FormatDateTime():
"ddd" returns "Fri"
"dddd" returns "Friday"
The main reason is that I want to obtain localized version of the 1 or 2 character day names:
Say FRIDAY in "ddd" would return:
French Windows = "Vendredi", the 2 char would be "VE", note it's the 1st and 2nd char.
Chinese Windows = "星期五", the char would be "五", note it's the 3rd char.
Japanese Windows = "金曜日", the char would be "金", note it's the 1st char.
Edit1:
Currently using Delphi, but i think applies to other languages too.
Edit2:
Simply put, I'm looking to obtain the shorter version of "ShortDayName" through the use of some functions or constants, so that I don't have to build a table of constants containing the 7 day "Shorter" day names for every possible windows language.
I wonder if such functions really exist.
Maybe the calendar 1 or 2 char day names in Outlook are hard-coded themselves, right?
You can get the local names for the days of the week with ShortDayNames and LongDayNames, and you can use DayOfWeek to get the numeric value for the day.
ShortDayNames[Index]; //Returns Fri
or
LongDayNames[Index]; //Returns Friday
The only way I know to shorten them to two chars would be to trim the resulting string
LeftStr(LongDayNames[Index],2);//Returns Fr
So today's Day would be
LeftStr(LongDayNames[DayOfWeek(date)],2); //Returns Fr
Click Here
Depicts the standards in custom date formatting.
You may also use the 'ddd' standard and trim it.
Delphi's routines does nothing special - they just ask OS.
Here is how to to it: Retrieving Time and Date Information. I looked through MSDNs docs and found this.
Note, that there is no really such thing as "2 character day-name" or "3 character day-name" here. There are: native ("long" in Delphi), abbreviated ("short" in Delphi) or short (Vista and above, not present in Delphi) formats.
For example, abbreviated name of the day of the week for Monday: Mon (3 chars, en-US), Пн (2 chars, ru-RU).
So, you probably look for LOCALE_SSHORTESTDAYNAMEX format (which is called "short" by MSDN and doesn't appear in Delphi), but it is availavle only on Vista and above.
For example, the following code:
const
LOCALE_SSHORTESTDAYNAME1 = $60;
procedure TForm1.Button1Click(Sender: TObject);
begin
SetThreadLocale($409);
ShowMessage(
GetLocaleStr(GetThreadLocale, LOCALE_SSHORTESTDAYNAME1, '') + #13#10 +
GetLocaleStr(GetThreadLocale, LOCALE_SABBREVDAYNAME1, '')
);
end;
will show you:
Mo
Mon
But doing this for Russian will output:
Пн
Пн
Hope my edits make answer more clear ;)

Resources