Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #34768 > unrolled thread
| Started by | Dr J R Stockton <J.R.Stockton@physics.org> |
|---|---|
| First post | 2018-03-12 11:26 -0700 |
| Last post | 2018-03-15 10:50 -0700 |
| Articles | 17 — 5 participants |
Back to article view | Back to comp.lang.javascript
How to deal with "new Date()" error when running JScript in Windows Script Host ? Dr J R Stockton <J.R.Stockton@physics.org> - 2018-03-12 11:26 -0700
Re: How to deal with "new Date()" error when running JScript in Windows Script Host ? Jon Ribbens <jon+usenet@unequivocal.eu> - 2018-03-12 19:01 +0000
Re: How to deal with "new Date()" error when running JScript in Windows Script Host ? Dr J R Stockton <J.R.Stockton@physics.org> - 2018-03-12 16:01 -0700
Re: How to deal with "new Date()" error when running JScript in Windows Script Host ? Jon Ribbens <jon+usenet@unequivocal.eu> - 2018-03-13 12:06 +0000
Re: How to deal with "new Date()" error when running JScript in Windows Script Host ? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-03-13 13:19 -0700
Re: How to deal with "new Date()" error when running JScript in Windows Script Host ? Dr J R Stockton <J.R.Stockton@physics.org> - 2018-03-14 13:29 -0700
Re: How to deal with "new Date()" error when running JScript in Windows Script Host ? Julio Di Egidio <julio@diegidio.name> - 2018-03-14 14:18 -0700
Re: How to deal with "new Date()" error when running JScript in Windows Script Host ? Dr J R Stockton <J.R.Stockton@physics.org> - 2018-03-15 02:19 -0700
Re: How to deal with "new Date()" error when running JScript in Windows Script Host ? Dr J R Stockton <J.R.Stockton@physics.org> - 2018-03-15 02:35 -0700
Re: How to deal with "new Date()" error when running JScript in Windows Script Host ? Jon Ribbens <jon+usenet@unequivocal.eu> - 2018-03-15 11:38 +0000
Re: How to deal with "new Date()" error when running JScript in Windows Script Host ? Dr J R Stockton <J.R.Stockton@physics.org> - 2018-03-15 13:37 -0700
Re: How to deal with "new Date()" error when running JScript in Windows Script Host ? Jon Ribbens <jon+usenet@unequivocal.eu> - 2018-03-15 23:24 +0000
Re: How to deal with "new Date()" error when running JScript in Windows Script Host ? Dr J R Stockton <J.R.Stockton@physics.org> - 2018-03-16 14:14 -0700
Re: How to deal with "new Date()" error when running JScript in Windows Script Host ? Jon Ribbens <jon+usenet@unequivocal.eu> - 2018-03-16 23:34 +0000
Re: How to deal with "new Date()" error when running JScript in Windows Script Host ? Dr J R Stockton <J.R.Stockton@physics.org> - 2018-04-18 03:15 -0700
Re: How to deal with "new Date()" error when running JScript in Windows Script Host ? JJ <jj4public@vfemail.net> - 2018-04-18 22:58 +0700
Re: How to deal with "new Date()" error when running JScript in Windows Script Host ? Julio Di Egidio <julio@diegidio.name> - 2018-03-15 10:50 -0700
| From | Dr J R Stockton <J.R.Stockton@physics.org> |
|---|---|
| Date | 2018-03-12 11:26 -0700 |
| Subject | How to deal with "new Date()" error when running JScript in Windows Script Host ? |
| Message-ID | <928d7c15-93f7-46e3-b5ee-78cb19ae02d1@googlegroups.com> |
At present, the second Sunday of March having started, when running JScript in Windows (7 & 10) Script Host in the UK after 23:00 LCT, D = new Date() thinks that it is tomorrow already, which upsets my "show today's files" logic. The reason is that WSH believes that the UK uses the US DST clock rules. I expect the fault to go away on the last Sunday of March, and to reappear for a week from the last Sunday of October, etc. I believe that the same root error will occur in other places using EU Summer Time Rules - and it could occur elsewhere. So I want to fix the root problem by using, instead of "new Date()" a new function "NewDate()" which will get the date from the MS-DOS environment variable %date% or by executing the MS-DOS DIR command. Does anyone here know how to write that new function (and, if so, please demonstrate)? I have reason to think that it can be done using non-ECMA calls within WSH. -- (c) John Stockton, near London, UK. Using Google Groups. | Mail: J.R.""""""""@physics.org - or as Reply-To, if any. |
[toc] | [next] | [standalone]
| From | Jon Ribbens <jon+usenet@unequivocal.eu> |
|---|---|
| Date | 2018-03-12 19:01 +0000 |
| Message-ID | <slrnpadjke.31p.jon+usenet@sable.unequivocal.eu> |
| In reply to | #34768 |
On 2018-03-12, Dr J R Stockton <J.R.Stockton@physics.org> wrote:
> At present, the second Sunday of March having started, when running
> JScript in Windows (7 & 10) Script Host in the UK after 23:00 LCT,
> D = new Date() thinks that it is tomorrow already, which upsets my
> "show today's files" logic. The reason is that WSH believes that the
> UK uses the US DST clock rules. I expect the fault to go away on
> the last Sunday of March, and to reappear for a week from the last
> Sunday of October, etc.
>
> I believe that the same root error will occur in other places using
> EU Summer Time Rules - and it could occur elsewhere.
>
> So I want to fix the root problem by using, instead of "new Date()"
> a new function "NewDate()" which will get the date from the MS-DOS
> environment variable %date% or by executing the MS-DOS DIR command.
>
> Does anyone here know how to write that new function (and, if so,
> please demonstrate)? I have reason to think that it can be done
> using non-ECMA calls within WSH.
This seems to work for me:
var WshShell = new ActiveXObject('WScript.Shell')
var aDate = WshShell.Exec('cmd /c echo %date%').StdOut.ReadLine().split('/')
var dDate = new Date(aDate[2], aDate[1] - 1, aDate[0])
Mind you, just 'new Date()' seems to be working for me anyway.
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <J.R.Stockton@physics.org> |
|---|---|
| Date | 2018-03-12 16:01 -0700 |
| Message-ID | <0a336b95-6961-4150-b149-9c97f1a66e94@googlegroups.com> |
| In reply to | #34769 |
On Monday, March 12, 2018 at 7:01:42 PM UTC, Jon Ribbens wrote:
> On 2018-03-12, Dr J R Stockton <see sig> wrote:
> > At present, the second Sunday of March having started, when running
> > JScript in Windows (7 & 10) Script Host in the UK after 23:00 LCT,
> > D = new Date() thinks that it is tomorrow already, which upsets my
> > "show today's files" logic. The reason is that WSH believes that the
> > UK uses the US DST clock rules. I expect the fault to go away on
> > the last Sunday of March, and to reappear for a week from the last
> > Sunday of October, etc.
> >
> > I believe that the same root error will occur in other places using
> > EU Summer Time Rules - and it could occur elsewhere.
> >
> > So I want to fix the root problem by using, instead of "new Date()"
> > a new function "NewDate()" which will get the date from the MS-DOS
> > environment variable %date% or by executing the MS-DOS DIR command.
> >
> > Does anyone here know how to write that new function (and, if so,
> > please demonstrate)? I have reason to think that it can be done
> > using non-ECMA calls within WSH.
>
> This seems to work for me:
>
> var WshShell = new ActiveXObject('WScript.Shell')
> var aDate = WshShell.Exec('cmd /c echo %date%').StdOut.ReadLine().split('/')
> var dDate = new Date(aDate[2], aDate[1] - 1, aDate[0])
>
> Mind you, just 'new Date()' seems to be working for me anyway.
Well, you've not said where you are, though I think you are UK.
Looking at the date alone, the code can only be properly tested
after 23:00 try just 'new Date()' then ...
Your code does do what you think, and I thought, was wanted ...
but your Windows is set to use date field order D/M/Y, whereas
I always follow ISO 8601 and use YYYY-MM-DD. I have easily enough
changed the code to accept any likely delimiter, and I now
distinguish Y M D and D M Y by the size of the Y field. But
I'd prefer it to work on any reasonable PC, and I fear my Mexican
neighbour might use M D Y.
I now recall another way of getting the date/time from Windows at
a Prompt
... ...
MS-DOS Prompt> wmic OS Get localdatetime /value
LocalDateTime=20180312214139.165000+000
and in WSH
var Line = WWshShell.Exec('cmd /c wmic OS Get localdatetime /value | find "Local"').StdOut.ReadLine()
LocalDateTime=20180312214947.234000+000
which I can easily enough process to the required form when
I find out what that really is. I think I've done that; but I'll
sleep on it before testing fully.
Thanks for including the key help in your answer!
H'mmm - there should be an API for using WMIC directly from JScript?
--
(c) John Stockton, near London, UK. Using Google Groups. |
Mail: J.R.""""""""@physics.org - or as Reply-To, if any. |
[toc] | [prev] | [next] | [standalone]
| From | Jon Ribbens <jon+usenet@unequivocal.eu> |
|---|---|
| Date | 2018-03-13 12:06 +0000 |
| Message-ID | <slrnpaffm2.31p.jon+usenet@sable.unequivocal.eu> |
| In reply to | #34770 |
On 2018-03-12, Dr J R Stockton <J.R.Stockton@physics.org> wrote:
> H'mmm - there should be an API for using WMIC directly from JScript?
Something like this perhaps:
var oWMI = GetObject('winmgmts:')
var eOSes = new Enumerator(oWMI.InstancesOf('Win32_OperatingSystem'))
var oOS
while (!(oOS && oOS.Primary) && !eOSes.atEnd()) {
oOS = eOSes.item()
eOSes.moveNext()
}
var oDate = new ActiveXObject('WbemScripting.SWbemDateTime')
oDate.Value = oOS.LocalDateTime
WScript.Echo(oDate.Day + '/' + oDate.Month + '/' + oDate.Year)
I'm not sure if bothering to search the operating system list is
overkill or if just
var oOS = new Enumerator(oWMI.InstancesOf('Win32_OperatingSystem')).item()
would be sufficient but presumably it doesn't hurt.
[toc] | [prev] | [next] | [standalone]
| From | "Michael Haufe (TNO)" <tno@thenewobjective.com> |
|---|---|
| Date | 2018-03-13 13:19 -0700 |
| Message-ID | <9e440780-c186-40b3-b288-a10a6ea16be7@googlegroups.com> |
| In reply to | #34770 |
Dr J R Stockton wrote:
> H'mmm - there should be an API for using WMIC directly from JScript?
<script>
var wmi = GetObject("WINMGMTS:\\\\.\\ROOT\\cimv2:Win32_OperatingSystem=@")
WScript.Echo(wmi.LocalDateTime)
</script>
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <J.R.Stockton@physics.org> |
|---|---|
| Date | 2018-03-14 13:29 -0700 |
| Message-ID | <f10a7ef6-ae3e-40dc-8787-7a1438e424e2@googlegroups.com> |
| In reply to | #34774 |
On Tuesday, March 13, 2018 at 8:19:42 PM UTC, Michael Haufe (TNO) wrote:
> Dr J R Stockton wrote:
>
> > H'mmm - there should be an API for using WMIC directly from JScript?
>
> <script>
> var wmi = GetObject("WINMGMTS:\\\\.\\ROOT\\cimv2:Win32_OperatingSystem=@")
> WScript.Echo(wmi.LocalDateTime)
> </script>
That's nicer. Thanks.
If course, it is not sufficient that the code chosen does
NOT apply US DST rules; it is also important that it DOES
apply the correct local Summer Time rules - I'll have to
wait another ten days or so to verify that. The present
replacement for new Date() follows ...
function NewDate() { var WMIC, LDT, M // c. 2018-03-13
// Use for 'new Date()' as in UK WSH JS uses USA DST rules :-(
/// M = 'cmd /c wmic OS Get localdatetime /value | find "Local"'
/// WMIC = WshShell.Exec(M).StdOut.ReadLine()
/// After Jon Ribbens in CLJ
/// Michael Haufe wrote in CLJ :-
/// var wmi = GetObject("WINMGMTS:\\\\.\\ROOT\\cimv2:Win32_OperatingSystem=@")
/// WScript.Echo("##### New: " + wmi.LocalDateTime) // JRS textmod
/// so
M = "WINMGMTS:\\\\.\\ROOT\\cimv2:Win32_OperatingSystem=@"
WMIC = GetObject(M).LocalDateTime // After Michael Haufe in CLJ
// WScript.Echo(" :: WMIC = " + WMIC)
M = WMIC.match(/(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)\.(\d\d\d)/)
// WScript.Echo(" :: M = " + M)
LDT = new Date(M[1], M[2]-1, M[3], M[4], M[5], M[6], M[7])
// WScript.Echo(" :: LDT = " + LDT)
return LDT } // Local DateTime Object from WMIC
--
(c) John Stockton, near London, UK. Using Google Groups. |
Mail: J.R.""""""""@physics.org - or as Reply-To, if any. |
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2018-03-14 14:18 -0700 |
| Message-ID | <09936528-6c1e-48e4-9045-4434fcaa7952@googlegroups.com> |
| In reply to | #34768 |
On Monday, 12 March 2018 19:26:33 UTC+1, Dr J R Stockton wrote: > At present, the second Sunday of March having started, when running > JScript in Windows (7 & 10) Script Host in the UK after 23:00 LCT, > D = new Date() thinks that it is tomorrow already, which upsets my > "show today's files" logic. Can you show code that reproduces the problem? > The reason is that WSH believes that the > UK uses the US DST clock rules. I expect the fault to go away on > the last Sunday of March, and to reappear for a week from the last > Sunday of October, etc. It's hardly possible that such a "feature" would have gone unnoticed. Julio
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <J.R.Stockton@physics.org> |
|---|---|
| Date | 2018-03-15 02:19 -0700 |
| Message-ID | <e2fc95ab-a6c1-4ad7-827d-6e812108284e@googlegroups.com> |
| In reply to | #34779 |
On Wednesday, March 14, 2018 at 9:18:19 PM UTC, Julio Di Egidio wrote:
> On Monday, 12 March 2018 19:26:33 UTC+1, Dr J R Stockton wrote:
>
> > At present, the second Sunday of March having started, when running
> > JScript in Windows (7 & 10) Script Host in the UK after 23:00 LCT,
> > D = new Date() thinks that it is tomorrow already, which upsets my
> > "show today's files" logic.
>
> Can you show code that reproduces the problem?
Yes.
> > The reason is that WSH believes that the
> > UK uses the US DST clock rules. I expect the fault to go away on
> > the last Sunday of March, and to reappear for a week from the last
> > Sunday of October, etc.
>
> It's hardly possible that such a "feature" would have gone unnoticed.
Never underestimate the chronological ineptitude of programmers on the wrong side of the Pacific Ocean.
Prompt>type jscptbug.js
// This tests JScript clock change dates; errors in Win 7 & 10
// Run in WSH at command line with Prompt>cscript //nologo jscptbug.js
// For a browser, remove use of WScript object and use alert(Out)
var IzzDat, IzzOff, WozDat, WozOff = NaN, J, Out = []
Out.push(" JSCPTBUG.JS\t(c) J R Stockton\t2018-01-29+")
Out.push(
"\n Your " + WScript.Name + " is version " + WScript.version +
"\n Your Script Engine is " + ScriptEngine() + " " +
ScriptEngineMajorVersion() + "." +
ScriptEngineMinorVersion() + "." + ScriptEngineBuildVersion() )
IzzDat = new Date()
Out.push(" They say :\n LCT = " + IzzDat.toString() +
"\n GMT = " + IzzDat.toUTCString())
Out.push(
" The following shows roughly when your civil clock offset\n" +
" changes over four years, according to your Script Engine.")
function Pad5(N) { var S = String(N)
while (S.length < 5) S = " " + S ; return S }
for (J = 0 ; J < 1475 ; J++) {
IzzDat = new Date(2017, 0, J) ; IzzOff = IzzDat.getTimezoneOffset()
if (J > 0 && IzzOff != WozOff) {
Out.push("")
Out.push(Pad5(WozOff) + " " + WozDat)
Out.push(Pad5(IzzOff) + " " + IzzDat)
}
WozOff = IzzOff ; WozDat = IzzDat
}
Out = Out.join("\n") + "\n -----"
// alert(Out)
WScript.StdOut.WriteLine(Out)
// End
Prompt>