I am trying to use jQuery/JavaScript to convert a date. I have turned the string into a object, but I just don't know how to get the results that I am looking for.
var dateObj = new Date(dateStr);
var dateFormatted = ???
How can I format a date such as 10/12/2014 into this Sun Oct 12 2014 00:00:00 GMT-0700 (PDT) in jQuery/JavaScript?
It would be easiest to use a library that is already written to modify dates. I would suggest moment.js, which can be found at momentjs.com. Then you can just write something like:
moment().format('MMMM Do YYYY, h:mm:ss a'); // July 9th 2014, 2:32:08 pm
If you use moment.js and want your desired format which I know is the HttpDate format the correct syntax is
var nowInHttpDate = moment().format("ddd, DD MMM YYYY HH:mm:ss ZZ");
I would recommend just using new Date() and passing in your string. The Date constructor has quite a bit of magic in regards to the formats it can take as an input. This should work:
dateStr = '10/12/2014';
date = new Date(dateStr);
console.log(date); // logs Sun Oct 12 2014 00:00:00 GMT-0700 (PDT)
Hopefully that works for you!
Related
I am having trouble with converting a datetime to the proper timezone
I do not understand why this is functioning like this.
d = "Thu Apr 26 2018 21:09:11 GMT-0700 (Pacific Daylight Time)"
moment.tz(d.toString(), this._timezone).format('MM/DD/YYYY h a')
returns 04/26/2018 3 pm
moment.tz(d, this._timezone).format('MM/DD/YYYY h a')
returns 04/26/2018 10 pm
Also moment.isMoment(d) returns false
also if I convert d to an ISO string before adjusting the TZ the TZ doesn't adjust
var d = "Thu Apr 26 2018 21:09:11 GMT-0700 (Pacific Daylight Time)";
console.log(moment.tz(d.toString(), 'America/Chicago').format('MM/DD/YYYY h a'));
console.log(moment.tz(d, 'America/Chicago').format('MM/DD/YYYY h a'));
console.log(moment.isMoment(d));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment-with-locales.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.16/moment-timezone-with-data.js"></script>
Automatic detection of non-ISO strings has been deprecated. See here for more information. The salient point is:
This deprecation warning is thrown when no known format is found for a
date passed into the string constructor. To work around this issue,
specify a format for the string being passed to moment().
So if you want to reliably parse the given string, you'll need to specify the format at parse-time as follows:
moment.tz(d, '<format here>', this._timezone);
I'm not sure exactly how to format your whole date correctly, but something like this should work:
var DATE_FORMAT = 'ddd MMM DD YYYY HH:mm:ss [GMT]Z'
var d = "Thu Apr 26 2018 21:09:11 GMT-0700 (Pacific Daylight Time)";
var DATE_FORMAT = "ddd MMM DD YYYY HH:mm:ss [GMT]Z"
console.log(moment.tz(d.toString(), DATE_FORMAT, 'America/Chicago').format('MM/DD/YYYY h a'));
console.log(moment.tz(d, DATE_FORMAT, 'America/Chicago').format('MM/DD/YYYY h a'));
console.log(moment.isMoment(d));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment-with-locales.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.16/moment-timezone-with-data.js"></script>
#Rajits information is correct but it was not the cause of the "odd" behavior.
although Thu Apr 26 2018 21:09:11 GMT-0700 (Pacific Daylight Time)" looks like a string it was actually an instance of a javascript date object.
new Date() was called on a UTC timestamp which resulted in that "string" therefore when passing the date instance directly to moment, moment could handle it but when turning it into a string with toString() it was changed into just a plain string.
Also calling new Date() on a UTC timestamp it automatically convert this timestamp into the browsers (clients computer) local timezone. So if you want to change your timestamp to an arbitrary timezone make sure not to call new Date() before.
how can you filter date on a JSON time like created_at like 2016-02-29 19:20:00 ?
The Angular filter only works on timestamp.
How can i change the output to the timestamp?
If I understand your question you want to do manipulate the created_at time or w/e time. I would highly suggest using moment.js for this.
Few examples from their page.
moment().format('MMMM Do YYYY, h:mm:ss a'); // February 28th 2016, 2:17:50 am
moment().format('dddd'); // Sunday
moment().format("MMM Do YY"); // Feb 28th 16
moment().format('YYYY [escaped] YYYY'); // 2016 escaped 2016
moment().format();
Same problem with me, below my solution:
In Json:
data[i].itemDate = data[i].itemDate.replace(/(.+) (.+)/, "$1T$2Z")
data[i].itemDate = new Date(data[i].itemDate);
Now you can use Angular filter
if you use YYYY-MM-DDTHH:mm:ss.sssZ time format you must be shure in a itemDate in UTC time zone. If your itemDate in GMT+0200 (for example) just drop "Z" from end, it helps to me :)
itemDate.replace(/(.+) (.+)/, "$1T$2")
I'm trying to parse a date in momentjs, in particular this is my goal:
Ven Nov 13 2015 09:00:00
Now I'm using FullCalendar and when I get the .start date it's returned this:
Fri Nov 13 2015 00:00:00
how you can see in my bottom code, I'm format the calendarDateStartTemp to utc for remove the GMT. In the next step, I transform the object in italian timezone, but this seems not working. Anyway, I've in workingPlan[selDayName].start the hour to edit, in particular this is the value: 09:00:00, see the code:
var calendarDateStartTemp = $calendar.fullCalendar('getView').start;
var calendarDateStart = moment(calendarDateStartTemp).utc().format("ddd MMM DD YYYY HH:mm:ss");
var calendarDateEnd = moment.lang('it');
calendarDateEnd = moment(moment(calendarDateStart).format("YYYY-MM-DD") + ' '
+ workingPlan[selDayName].start).format('ddd, D MMM YYYY HH:mm:ss');
now the problem's that I get this result:
Fri, 13 Nov 2015 09:00:00
instead of this:
Ven Nov 13 2015 09:00:00
how you can see the date returned is in english language, but I don't know why moment.lang now working. I say that it's deprecated so I've also tried with moment.locale but I've the same problem. How I can fix this?
NB: the language is italian
var data = moment().locale('it').format('llll');
alert(data);
By default, Moment.js comes with English locale strings. If you need other locales, you can load them into Moment.js.
I'm assuming you have both moment.js and monement+locales.js included, the scripts are found here. http://momentjs.com/
I need to convert a string to a JavaScript-Date
Examples of the source-strings
Wed, 01 Apr 2015 07:30:42 CEST
Mon, 23 Mar 2015 08:00:15 CET
I tried parsing it with Date.parse and creating a new Date from it but it is not working, JS throws an "Invalid Date" error.
What is the best way to convert this string? Do I have to go all RegEx over it?
Thanks in advance!
Your timezones are not valid for Date.parse()
So you have to replace your timezones, something like this:
var dateString = "Wed, 01 Apr 2015 07:30:42 CEST";
dateString = dateString.replace("CET", "GMT+1");
dateString = dateString.replace("CEST", "GMT+2");
var date = Date.parse(dateString);
console.log(date);
// => 1427873322000
I have a datetime of the form:
var myDate = "2013-06-07T00:00:00.000Z"
I wish to do
jQuery.datepicker.parseDate( "yy-mm-dd", myDate);
I don't care about the time part.
I get:
"Extra/unparsed characters found in date: T00:00:00.000Z"
Best I got so far is:
myDate = myDate.replace('T00:00:00.000Z', '');
myDate = jQuery.datepicker.parseDate("yy-mm-dd", myDate).toUTCString();
Please help.
As it is ISO date format, I think you can call new Date(myDate) directly there is no need to parse it I think
var date = new Date(myDate);
If you don't care about the time part, why not simply
jQuery.datepicker.parseDate( "yy-mm-dd", myDate.split("T")[0]);
Perhaps for general DateTime handling, have a look at
moment.js
You can use split
var myDate = "2013-06-07T00:00:00.000Z";
var n=myDate.split("T");
console.log(n); // Pass the date part only to date picker
By modifying the String used to describe the format, you can get it to do this (assuming time is always zero)
var myDate = "2013-06-07T00:00:00.000Z",
d = jQuery.datepicker.parseDate("yy-mm-ddT00:00:00.000Z", myDate);
d; // Fri Jun 07 2013 00:00:00 GMT+0100 (GMT Daylight Time)
However, this will ignore the fact that Z denotes timezone UTC and instead uses local timezone (in my case BST/GMT+1). You can repair this quickly though.
d.setMinutes(d.getMinutes() - d.getTimezoneOffset()); // or use UTCMinutes
d; // Thu Jun 07 2013 01:00:00 GMT+0100 (GMT Daylight Time)
// which is now correct in terms of timezone
If you want something that will change your life, just use http://momentjs.com/... It's the equivalent of Carbon for PHP. Simple, multi-language and cross-browsers.
For your question, it goes :
moment("2013-06-07T00:00:00.000Z").format('YY-MM-DD')
Hope this helps future visitors.
What about this:
jQuery.datepicker.parseDate( "yy-mm-dd", myDate.substr(0, 10) );