This question already has answers here:
javascript: how to parse a date string
(4 answers)
Closed 8 years ago.
I get a date returned from an API in the format:
var date = result.posted //gives 2014-03-29 02:07:26
when I run
new date(date)
on desktop it works fine
but on mobile, I get the error 'invalid date'
how can I make this work across the board - I want to compare it to the current time ( var cur_time = new date() )
I suggest you to use moment.js, it is very easy to compare date. In your case:
moment("2014-03-29 02:07:26", "YYYY-MM-DD HH:mm:ss").fromNow();
Try this..
var date = new Date(Date.parse(parseInt(result.posted)))
it will give date object like below
Date {Wed Jan 01 2014 05:30:00 GMT+0530 (India Standard Time)}
Related
This question already has answers here:
Incorrect date shown in new Date() in JavaScript
(3 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 2 years ago.
I have HTML Date inputs that return a string in the format of 'YYYY-MM-DD'
I then want to put this into the Date Constructor so that I can get it converted into ISOFormat (for use of Mongo Querying)
However, the results are unexpected.
Doing new Date('2020-06-25') returns "Wed Jun 24 2020 20:00:00 GMT-0400 (Eastern Daylight Time)". Note that this is the day prior. Why does this happen and how can I change this to get the current date? THanks
When you create a new Date object in JavaScript, it expects the argument to be in GMT. It then converts it to your local timezone GMT-0400, which explains why its 4 hours off. You can set the timezone by appending it to the date string: new Date('2020-06-25 GMT-0400') should give you "Thu Jun 25 2020 00:00:00 GMT-0400".
This question already has answers here:
How to initialize a JavaScript Date to a particular time zone
(20 answers)
How do I specify the time zone when creating a JavaScript Date?
(1 answer)
Closed 5 years ago.
Without using moment.js, is there a way to construct a date in a specific timezone.
For example, I have two strings:
var date = '2018-01-23';
var time = '12:00';
I can construct the date like so:
var constructedDate = new Date(date.substring(0,4), date.substring(5,7)-1, date.substring(8,10), time.substring(0,2), time.substring(3,5));
which provides output of:
Tue Jan 23 12:00:00 GMT+00:00 2018
However, I am looking for output in a particular timezone (e.g +11:00)
Tue Jan 23 12:00:00 GMT+11:00 2018
Alternatively, is there a way to offset the date -11 hours so when I use the GMT date in my application it will be correct.
This question already has answers here:
“Deprecation warning: moment construction falls back to js Date” when trying to convert RFC2822 date in moment.js
(7 answers)
Closed 6 years ago.
I have a date string like this: "Mon Jul 18 2016 21:35:14 GMT+00:00" and would like to use this in moment to format it to "MM-DD-YYYY", it does that but throws a warning pointing back to this issue https://github.com/moment/moment/issues/1407
Is there a way that I can convert the above to a date object and then use it in moment for formatting something like:
moment(Mon Jul 18 2016 21:35:14 GMT+00:00).format('MM-DD-YYYY');
You should include the string and the format within the moment call:
var date_as_string = "Mon Jul 18 2016 21:35:14 GMT+00:00";
var current_format = "ddd MMM DD yyyy HH:mm:ss Z";
moment(date_as_string, current_format).format('MM-DD-YYYY');
You are telling the function what format your string is in so that it can parse it correctly.
This question already has answers here:
Is the Javascript date object always one day off?
(29 answers)
Closed 6 years ago.
var dateTest = new Date('2015-03-31');
console.log(dateTest);
Result:
Mon Mar 30 2015 20:00:00 GMT-0400 (Eastern Daylight Time)
However I expected this to be March 31. How might I do this?
Because it's setting the date according to UTC, and returning it with your local timezone offset. I'd recommend explicitly specifying the timezone offset, if that's what you need, or adding your timezone offset after the fact.
var d = new Date('2015-03-31T00:00:00-0400');
// or
var d = new Date('2015-03-31');
d.setMinutes(d.getMinutes() + d.getTimezoneOffset());
If you want it to be in UTC, you can call the toUTCString() method on it instead, which will give you the date you expect, albeit not in your timezone.
This question already has answers here:
Get String in YYYYMMDD format from JS date object?
(53 answers)
Closed 6 years ago.
I'm trying to get the first and last day of the month with the format 2016.02.29, but I'm not sure how to turn the date into this format.
The following code (taken from here):
var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
Gives this format: Mon Feb 29 2016 00:00:00 GMT+0100 (W. Europe Standard Time).
How do i make it look like 2016.02.29? I could manipulate the string to get the result I want, but isn't there a way to get it by just defining a format?
You can use the MomentJs to format any date by specifying the exact format.
Load the momentjs or momentjs + locale script in your page
moment().format('YYYY.MM.DD');