We have a web application that gets its data from a certain database. The product writing to that database has been localized to RUSSIAN, thus its data, in particular the dates had been localized too.
We encountered a problem where our DATES would not show on our application. We traced the problem to an invalid Date.parse() javascript call.
Example:
<html>
<body>
<script type="text/javascript">
var value = Date.parse("01/31/2009 08:00:00 AM");
document.write(value);
</script>
</body>
Would return 1260576000000.
However,
<html>
<body>
<script type="text/javascript">
var value = Date.parse("31.01.2009 08:00:00 AM");
document.write(value);
</script>
</body>
Would return NaN.
Is there a way to parse localized dates in Javascript?
Thanks!
It looks like the sort of situation where I would find a friendly developer who knows Regex. Regex should be able to turn one type of date format to another before you parse the string.
The built-in function does not support i18n. Use a toolkit, auch as dojo, to parse and output dates.
Related
This may be an odd question, but I have a moment date which I will later need to grab parts of it for different usages. For example, I grab the day and display doing date.date() from moment.
All that is find and dandy, but the issue is timezones are relevant to this.
So I have my moment object, which is created from the string:
const properDate = moment.tz(date, 'YYYY-MM-DD hh:mm:ss.SSS', 'America/New_York')
From my understanding, for me to get the proper timezone time, I would do
properDate.format()
But this will convert it to a string. And I would like to be able to fetch the time and days on the correct timezone. So basically, when I do properDate.hours() I will get it in UTC's time, but I want it in America/New_York.
Say for example the date string I want to put in is 2018-12-31 02:00:00.000 +00:00
When I run it through the above code, and then do properDate.hours() I am going to get 2, but I would like to get it in the New York timezone, which would be 21, and would also be the previous day if I want to get the date.
I suppose a way around this would be to convert it to a date object using new Date and setting each one manually, perhaps? Something like this:
const dateWithTimezoneObject = new Date(properDate.year(), properDate.month(), properDate.date(), properDate.hours(), properDate.minutes())
But that sounds like a bit of a mess and I was wondering if there is a better way of getting this done, possibly through moment.
So, is there a way for me to get the parts of the moment object in the proper timezone time?
I believe what you're looking for is:
var m = moment("2018-12-31 02:00:00.000 +00:00","YYYY-MM-DD HH:mm:ss.SSS Z")
.tz('America/New_York');
m.hours() //=> 21
A few things:
Note the input format provided is case sensitive and needs to match the input string. In your question, you had hh instead of HH, and also didn't provide the Z for the time zone offset.
There are two different flavors of the tz function. moment.tz() is for constructing a moment where the input value is in terms of that time zone. moment().tz() is for converting a moment to a different time zone.
Though I just used moment in my example above, you could use moment.tz, moment.utc or moment.parseZone and the effect would be the same, but only because you included the offset +00:00 in the input string. If you don't actually have that offset, then you'd want to use moment.utc, like this:
var m = moment.utc("2018-12-31 02:00:00.000","YYYY-MM-DD HH:mm:ss.SSS")
.tz('America/New_York');
Regarding your proposed solution, that would be creating a Date object representing a different point in time. In general, once you are using Moment you should avoid using the Date object if you can use a Moment function instead.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
Test
<script src="moment.js"></script>
<script src="moment-timezone-with-data.js"></script>
<script>
const date = '2018-12-31 02:00:00.000 +00:00'
const new_york = moment(date,'YYYY-MM-DD HH:mm:ss.SSS Z')
.tz('America/New_York')
document.body.textContent =
`day: ${new_york.dates()} hour: ${new_york.hours()}`
</script>
</body>
</html>
https://momentjs.com/timezone/
I need to change my date format comes from database,for that I have seen many solutions but I need certain solution. here given my date
2015-08-15 02:54:43
I need to change this date into Aug-8 02:54 AM.
Please, provide me the certain solution
Thank you
Yes you can format Date in SQL query also, but if there is situation where you have to format in jquery then you can use:
Moment
Its a plugin, to Parse, validate, manipulate, and display dates in
JavaScript.
Instance:
$(function(){
var divLocal = $('#divLocal');
var localTime = moment("2015-08-15 02:54:43").toDate();
localTime = moment(localTime).format('MMMM-DD h:mm:ss A');//August-15 2:54:43 AM
//localTime = moment(localTime).format('MMMM-MM h:mm: A');//output August-08 2:54:43 AM
divLocal.text(localTime);
});
DEMO
you need to use date.js here below I have given an example
first of all include following both js in your page
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="http://www.datejs.com/build/date.js"></script>
then
write following script bottom of the page
<script>
var date = '2015-08-15 02:54:43' //your date
var parseDate = Date.parse(date);
alert(parseDate.toString("MMM-d hh:mm tt"));
</script>
I am parsing 2 different date strings
var d1 = '2014-02-01T00:00:00.000+0530'
var d2 = '2014-02-23T00:00:00.000+0530'
when i parse them using moment
alert(moment(d1, 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"').toDate());
alert(moment(d2, 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"').toDate());
both of them print Sat Feb 1 2014 xxxxx
what is wrong with it??
here is the link to the fiddle i created
jsfiddle
I think your moment formatting string is causing you the problem. If I remove this, the dates do not print as the same.
http://jsfiddle.net/K5ub8/7/
EDIT: The specific issue is you are using dd for day, instead of DD. http://momentjs.com/docs/#/parsing/string-format/
Here is your fiddle fixed:
http://jsfiddle.net/K5ub8/9/
However, I am not 100% sure about the fractional seconds, I believe it is SSS instead of fffffff but I would test this if you need to cater for fractional seconds.
I should mention that if you are converting it back into a JavaScript date object anyway with toDate(), then you don't really need the moment formatting parameter as the date will be formatted in JSON Date format.
I would question why you would want to generate a moment formatted date, and then convert it back to JavaScript, a normal practice might be to receive a date in JavaScript format, then create a moment object which you can use to perform calculations and display in a nice user friendly way.
Simple answer: your format was off a bit.
http://jsfiddle.net/K5ub8/8/
After tweaking the format to be 'YYYY-MM-DDTHH:mm:ss.SSSZZ' rather than 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"' it worked just fine. When you're trying to debug issues like this, it's always good to keep the format in a separate variable so you can use the same format that you're trying to parse out to display what you're getting. Had you done that, you would have noticed that 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"' was messed up due to it printing out 2014-01-Fr"T"11:32:03.fffffff"-08:00". Which obviously isn't quite right.
I'm trying to use MomentJS to support dates handling in my application. However, I'm facing a problem with date manipulation.
The files are loaded in this order:
<script src="/javascripts/modules/moment/moment.min.js"></script>
<script src="/javascripts/modules/moment/moment-timezone.min.js"></script>
<script src="/javascripts/modules/moment/moment-timezone-data.js"></script>
<script src="/javascripts/modules/moment/moment-with-langs.min.js"></script>
Now in somepart of my JS code I change the moment language to FR or PT.
moment.lang('fr');
Both languages validade a date as "DD/MM/YYYY" instead of american pattern. So I expect moment
to validate a date following the country date pattern passed.
Then 12/10/2014 must be: day (12), month (09), year (2014), but it is returning always american pattern instead of the correct one.
I'm getting the date properties as:
console.log("DAY: " + moment(textDate).date());
console.log("MONTH: " + moment(textDate).month());
console.log("YEAR: " + moment(textDate).year());
where textDate is my date taken from a text input.
## EDIT ##
I know I can pass the pattern to Moment. I.e:
moment(textDate, 'DD/MM/YYYY');
In the case of my application I'm using like this:
moment(textDate, '<%=lingua.general.time.pDate%>');
However, it suppose to work automatically, don't it? Of course if you already have needed language packages as well. So the previous way I mentioned before should Works, whatever.
If you don't pass any formatting arguments, moment will let your browser do the parsing (with the exception of a full ISO timestamp).
To tell moment to do the parsing, and to use the localized short date format associated with the language, pass an L as the format string:
moment(textDate, 'L')
See in the docs:
Parsing using #String+Format
The display formats. Scroll down to "Localized formats". (The parser uses the same format strings)
Also, not related to your question, but moment-with-langs already includes a copy of moment.js, so you don't need both scripts.
Dates are always a kick in the nuts (at least for me) and I faced the fact that Javascript doesn't seem to have a method to format dates.
I'm trying to format a date to use the Google API which ask you for a date in this format: yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffff (e.g. 2011-06-25T10:00:00.000+02:00)
I need to be able to read that kind of string and to produce one.
I often use jQuery plugin: http://plugins.jquery.com/project/jquery-dateFormat
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="https://raw.github.com/phstc/jquery-dateFormat/master/jquery.dateFormat-1.0.js"></script>
<script type="text/javascript">
$(function() {
var d = new Date();
alert($.format.date(d, 'yyyy-MM-ddTHH:mm:ss'));
})
</script>