below is the code of VBScript,
FromDate=22/2/2013
ToDate= 1/3/2013
StartDateSerial = DateSerial(year(FromDate),month(FromDate), day(FromDate))
ToDateSerial = DateSerial(year(ToDate),month(toDate),day(ToDate))
in this date format is dd/mm/yyyy. but result of DateSerial is as below
StartDateSerial= 2/22/2013
ToDateSerieal= 1/3/2013
which mean for start date it converted to mm/dd/yyyy but for ToDateSerieal it converts to dd/mm/yyyy
I found this issue on US lacal. But At UK local this is same for both.
Now when I try this with following values in FromDate and To Date
FromDate=2/22/2013
ToDate= 3/1/2013
I am passing FromDate and ToDate from JavaScript to vbscript.
Format is mm/dd/yyyy then it works fine on US lacal but not on UK local. Can any body tell me how can I fix this.
I assume, you pass d/m/y strings from Javascript to VBScript running with US locale which expects m/d/y date strings. If presented with a bad date string, VBScript tries to do the right thing and converts "22/2/2013" to a february date; "1/3/2013" is seen as a valid january date. UK locale understands d/m/y strings, "1/3/2013" is a march date.
To solve your problem - d/m/y date string input for all locales - use a = Split("d/m/y", "/") on the strings and DateSerial(a[2], a[1], a[0]).
To make sure you are working on the correct locale, you can use SetLocale to set the locale. For dd/mm/yyyy you can use 2057, the English UK locale.
cl = GetLocale()
SetLocale(1033) ' US locale
wscript.echo FormatDateTime("1/3/2013",1)
SetLocale(2057) ' UK locale
wscript.echo FormatDateTime("1/3/2013",1)
' Set back the original locale
SetLocale(cl)
' Output:
' Thursday, January 03, 2013
' 01 March 2013
Related
I have a date in local format , I changed it to UTC format . After UTC i want to extract date and time in UTC . But whenever I am trying to extract time or date for that matter , it is considering local time only , why is that ?
this.utcDate = this.datePipe.transform(this.onceDate,'medium');
console.log('+=timezonenene=+', this.utcDate);
this.utcDateStr = new Date(this.utcDate).toUTCString();
console.log('++ date is ++', this.utcDateStr);
this.utcDateStr = new Date(this.utcDateStr).toISOString();
console.log('++ date is ++', this.utcDateStr);
this.showDate = this.datePipe.transform(this.utcDateStr, 'yyyy-MM-dd');
this.showTime = this.datePipe.transform(this.utcDateStr, 'HH:mm');
console.log('+=time date zonenene date str =+', this.showDate);
console.log('+=timezonenene date str=+', this.showTime);
output :
+=timezonenene=+ Apr 7, 2020, 5:41:16 PM
++ date is ++ Tue, 07 Apr 2020 12:11:16 GMT
++ date is ++ 2020-04-07T12:11:16.000Z
+=time date zonenene date str =+ 2020-04-07
+=timezonenene date str=+ 17:41
I want last logged time as : 12:11 not 17:41
Your question appears to be about the Angular DatePipe (though you're calling the transform function directly).
From the docs about the timezone parameter:
When not supplied, uses the end-user's local system timezone.
Thus, to get UTC output, you can change your code to provide the time zone parameter 'UTC' after the format parameter, on each call to datePipe.transform.
Also, I'd reconsider why you are producing strings just to parse them again. Usually this is unnecessary, and can lead to errors. if this.onceDate is a Date object, you should be able to use that throughout your code, rather than creating this.utcDateStr just to parse and transform it again. (Though it's difficult to tell from your code if that is the case. In the future, please supply a minimal, reproducible example.)
Trying to understand how moment.js is converting a string to date, I've bounced into this issue.
let date = "User has logged in to more than 10 .";
console.log(moment(date)); //output date
let invalid = "User has logged in to more than 10 a";
console.log(moment(invalid)); //output invalid date
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.js
"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-range/2.2.0/moment-range.js"></script>
can someone explain it to me ??
CodePen link
When you pass the string moment checks whether it is a valid date format, and if not, it falls back to the built-in javascript Date.parse() method.
The moment.js docs say:
When creating a moment from a string, we first check if the string
matches known ISO 8601 formats, we then check if the string matches
the RFC 2822 Date time format before dropping to the fall back of new
Date(string) if a known format is not found.
Date.parse does not recognize anything useful in your string until it encounters 10; it drops the rest. A default date format is assumed, which will depend on your location and language. In my own case, here in the US, the format is MM/DD. The result is that the string is parsed to a date of Oct. 1st (10th month, no day specified defaults to the 1st). And then (for Y2K-ish reasons, I suspect) it assumes a year of 2001, since no year is given.
We get the same behavior from javascript's built-in Date methods:
new Date(Date.parse('User has logged in to more than 10.'))
// Mon Oct 01 2001 00:00:00 GMT-0400 (EDT) <- As printed from Michigan.
In your second case, you tried ending the string with 10 a instead of 10 . and you will notice the same behavior (invalid date) if you pass the same to the built-in Date methods.
I am trying to convert datetime value from this format Wed Mar 9 09:48:09 PST 2016 into the following format YYYY-MM-DD HH:mm:ss
I tried to use moment but it is giving me a warning.
"Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.
Arguments: [object Object]
fa/<#http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:9493
ia#http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:10363
Ca#http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:15185
Ba#http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:15024
Aa#http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:14677
Da#http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:15569
Ea#http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:15610
a#http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:41
#http://localhost:1820/Home/Test:89:29
jQuery.event.dispatch#http://localhost:1820/Resources/Scripts/Jquery/jquery.min.js:5225:16
jQuery.event.add/elemData.handle#http://localhost:1820/Resources/Scripts/Jquery/jquery.min.js:4878:6
"
according to https://github.com/moment/moment/issues/1407 I should not be trying to use moment() to do this since it is not reliable.
How can I reliably convert the Wed Mar 9 09:48:09 PST 2016 into the following format YYYY-MM-DD HH:mm:ss?
You could try using Date.toJSON() , String.prototype.replace() , trim()
var date = new Date("Wed Mar 9 09:48:09 PST 2016").toJSON()
.replace(/(T)|(\..+$)/g, function(match, p1, p2) {
return match === p1 ? " " : ""
});
console.log(date);
Since you tagged your question with moment, I'll answer using moment.
First, the deprecation is because you are parsing a date string without supplying a format specification, and the string is not one of the standard ISO 8601 formats that moment can recognize directly. Use a format specifier and it will work just fine.
var m = moment("Wed Mar 9 09:48:09 PST 2016","ddd MMM D HH:mm:ss zz YYYY");
var s = m.format("YYYY-MM-DD HH:mm:ss"); // "2016-03-09 09:48:09"
Secondly, recognize that in the above code, zz is just a placeholder. Moment does not actually interpret time zone abbreviations because there are just too many ambiguities ("CST" has 5 different meanings). If you needed to interpret this as -08:00, then you'd have to do some string replacements on your own.
Fortunately, it would appear (at least from what you asked) that you don't want any time zone conversions at all, and thus the above code will do the job.
I have a the locale and a date in that locale format stored in a var in JavaScript
I need to convert that locale based date string to another local format
I have
locale : en-GB / en-US / es / ko
date : dd/mm/yyyy / mm/dd/yyyy yyyy.mm.dd
the above mentioned formats are not exact
ijust mean they are different for each
not i want that date to be displayed as "August 01 2013" like this
SO Finally i need a function(fromLocale,ToLocale,dateInFromLocaleFormat) which returns Date in ToLocale format
Can anyone help me on this
Can't you just use the JavaScript function dateFormat and set the format manually when you display the date?
for example: dateFormat("date", "mmmm dd yyyy");
more detailed here http://blog.stevenlevithan.com/archives/date-time-format
How does one convert a string of a date without a year to a JS Date object? And how does one convert a date string with a year and a time into a JS Date object?
Many different date formats can be converted to date objects just by passing them to the Date() constructor:
var date = new Date(datestring);
Your example date doesn't work for two reasons. First, it doesn't have a year. Second, there needs to be a space before "pm" (I'm not sure why).
// Wed May 27 2009 23:00:00 GMT-0700 (Pacific Daylight Time)
var date = new Date("2009/05/27 11:00 pm")
If the date formats you're receiving are consistent, you can fix them up this way:
var datestring = "05/27 11:00pm";
var date = new Date("2009/" + datestring.replace(/\B[ap]m/i, " $&"));
I'd use the Datejs library's parse method.
http://www.datejs.com/
I tried your example and it worked fine...
5/27 11:00pm
Wednesday, May 27, 2009 11:00:00 PM
I have used the Dojo time parser to do things like this:
Check it out:
http://api.dojotoolkit.org/jsdoc/HEAD/dojo.date.locale.parse
Not the cleanest, but works:
var strDate = '05/27 11:00pm';
var myDate = ConvertDate(strDate, '2009');
function ConvertDate(strWeirdDate, strYear)
{
strWeirdDate = strWeirdDate.replace(/ /, '/' + strYear + ' ');
return new Date(strWeirdDate);
}
Probably want to trim the string first as well.
Just another option, which I wrote:
DP_DateExtensions Library
It has a date/time parse method - pass in a mask and it'll validate the input and return a data object if they match.
Also supports date/time formatting, date math (add/subtract date parts), date compare, speciality date parsing, etc. It's liberally open sourced.