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>
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/
How can I change a date from the form 01/01/02 to January 1st, 2002?
Can I do this in javascript? If not, jQuery?
While you can certainly write your own date formatting routine, your best choice is using a library like moment.js. That is especially true if you'd be doing some manipulations to those dates besides just displaying them. Dates are hard to do and there are many things to consider (i.e. timezones)...
Using momentjs:
var x = moment('01/01/02', 'mm/dd/YYYY').format('MMM Do, YYYY');
console.log(x);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
dateObj.toLocaleDateString() in javascript might be helpful
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 want to be able to enter a UK formatted date when creating a new Date from a string, that will work in all modern browsers, but mainly FireFox and IE.
new Date('24/06/2014') // Gives 06/24/2014
Because I want to set a jquery datepicker using it.
Is it possible to get
new Date()
to interpret a UK date passed as a string?
new Date('24/06/2014').toLocaleString("en-GB") // Gives 06/12/2015
new Date('06/24/2014').toLocaleString("en-GB") // Gives 24/06/2014
The real problem is when I use the datepicker I specify UK date format and everything is fine...
$("#outOfStockFromDate").datepicker({
dateFormat: 'dd/mm/yy',
changeMonth: true,
numberOfMonths: 3,
onClose: function (selectedDate) {
$("#to").datepicker("option", "minDate", selectedDate);
}});
Until I try to repopulate the datepicker using this UK date format using something like...
$("#outOfStockFromDate").datepicker("setDate", testDate);
It's as if the string 'testDate' must be in US format... grrr
EDIT: I realise there are two parts to this question, the first part dealing with datepicker; it was my stupid fault that I was initialising it at the same time I was trying to setDate (this error led me down the Date() route). Now I have it initialised and changed correctly it accepts UK format dates!
Second part yes the date functionality in javascript regarding formatting dates (now I know) is famously lacking... but as I've solved my initial problem I don't care about this one... P.S. I ended up splitting and reconstructing the date in US Date Format (in a string) then using new Date(USDateFormattedString).... and then I realised!
So to end, essentially my code was correct in it's syntax, but in it's execution it was not.
Thanks to everyone who answered! +1
You can't get new Date to accept a string with a date in UK format directly, however, you can parse it manually as I've shown in another question with similar issue:
function parseDMY(value) {
var date = value.split("/");
var d = parseInt(date[0], 10),
m = parseInt(date[1], 10),
y = parseInt(date[2], 10);
return new Date(y, m - 1, d);
}
This version as is accepts dates like this one:
parseDMY("01/13/2014").toLocaleString("en-GB")
"1/1/2015 00:00:00"
You can add some validations if you want to make sure that doesn't happen.
Assuming you are using jquery-ui then I believe your missing the reference to the localization of the datepicker. For example:
$("#datepicker").datepicker( "option",$.datepicker.regional[ "en-GB" ] );
Here is a complete example:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Localize calendar</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
</head>
<body>
<p>Date: <input type="text" id="datepicker">
<button id="set">Test Set Date</button>
<button id="get">Test Get Date</button>
</body>
<script>
$(function() {
$( "#datepicker" ).datepicker({ dateFormat: "dd/mm/yy" });
$( "#datepicker" ).datepicker( "option",
$.datepicker.regional[ "en-GB" ] );
$("#set").click(function(){
$("#datepicker").datepicker("setDate", new Date());
});
$("#get").click(function(){
alert($("#datepicker").datepicker("getDate"));
});
});
</script>
</html>
Here is the documentation of jQuery UI localization.
All the best,
acg
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.