How to format the output of the javascript function date() ? [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to format javascript date
I have the following bit of code
$('#ins_date').attr('value', Date());
What i get is this : Wed Mar 21 2012 17:52:32 GMT+0100 (Romance Standard Time)
How could i format this string to get somethingh like this : 2012-03-01

date.js is the best I've found. It supports many different formats.

Best option for now is using moment.js library. Formatting for native Javascript Date is not well supported.
var now = moment();
now.format("YY-MM-DD");
$('#ins_date').attr('value', now);

Related

Convert Date to javascript format [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Parsing a string to a date in JavaScript
(35 answers)
Closed 2 years ago.
I am trying to convert this date into Mon Nov 26 2018 10:32:04 GMT (I am getting this data from the Api so i can't make changes to it)
I assume it is considering 26 as months thats why it is showing it as invalid date
Can Anyone help me with this. How to convert that date into the expected output i specified.
How to get
var d = new Date("26-11-2018 10:32:04")
return d; //Error: Invalid Date
expected Output: Mon Nov 26 2018 10:32:04 (IST)
Use moment.js to parse the date.
moment("26-11-2018 10:32:04", "DD-MM-YYYY HH-mm-ss").toDate()
Alternatively, if you really don't want to use moment for whatever reason, you can use regex magic.
new Date("26-11-2018 10:32:04".replace(/^(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)$/, "$3-$2-$1T$4:$5:$6Z"))
This is not a robust as #Yevgen answer but it also much simpler.
All I'm doing is removing the - and flipping the day and month values
const items = "26-11-2018 10:32:04".split('-')
new Date(`${items[1]} ${items[0]} ${items[2]}`)
This works for personal projects but I highly recommend using moment.js

In JavaScript, when do/don't Dates get converted? [duplicate]

This question already has answers here:
different result for yyyy-mm-dd and yyyy/mm/dd in javascript when passed to "new Date" [duplicate]
(2 answers)
Closed 6 years ago.
I'm confused because typing the same date in a different format results in two different date outputs, the first converted and the second, not. Here is the code:
var x = new Date("2015-03-25"); // outputs Tue Mar 24 2015 17:00:00 GMT-0700 (PDT)
var y = new Date("03/25/2015"); // outputs Wed Mar 25 2015 00:00:00 GMT-0700 (PDT)
The way dates are parsed by browsers is a huge pile of unpredictable inconsistency. You should not attempt it. Here's the full rundown in case you're curious: http://dygraphs.com/date-formats.html
If you want consistent parsing you should implement it yourself or us a library that does it. Momentjs is widely used: http://momentjs.com/

change this date format to work with new date() [duplicate]

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)}

format the date like 12:30 PM 12TH JANUARY 2000 in javascript [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 9 years ago.
I have to display the date in jsp 12:30 PM 12TH JANUARY 2000 in this format .How can i format the current date in the above format in JavaScript or using jQuery UI.
var currentDate = new Date();
currentDate.toString("hh:MM dS MMMM yyyy")
I have tried the above code but it's not working.
Strange as it seems, Javascript doesn't have a built-in function or method that lets you specify the date/time formatting.
But you can use something like this: https://github.com/samsonjs/strftime
jquery date format is separate plugin.U need to add explicitly using <script> tag.
Formatting using javascript
<script>
$.format.date("2009-12-18 10:54:50.546", "dd/MM/yyyy");
</script>
E.g.,
<script>
document.write($.format.date("2009-12-18 10:54:50.546", "Test: dd/MM/yyyy"));
document.write($.format.date("Wed Jan 13 10:43:41 CET 2010", "dd~MM~yyyy"));
</script>
See here

Converting JSON string into date [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert a Unix timestamp to time in Javascript
I'm trying to convert the string that this api returns for date. The format is 1351993013. I tried the following line of JS but the date is completely wrong.
var jsonDate = plugin.versions[0].date;
var pluginDate = new Date(jsonDate);
Which returns:
Fri Jan 16 1970
This is the first time I've tried to format a JSON date so it's a bit confusing. Can anyone help?
That would be seconds, and javascript uses milliseconds which would be
1351993013​000
which would give you Sunday Nov 04 2012.
in other words:
var jsonDate = parseInt(plugin.versions[0].date, 10) * 1000;

Categories

Resources