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 this date here 2013/06/10, which comes from the database and is set in a variable called date.
I added one day to this date by doing this..
var endDate = new Date(date);
endDate.setDate(endDate.getDate() + 1);
and now I am trying to change the format to yyyy/MM/dd
var finalEndDate = endDate.toString('yyyy/MM/dd');
alert(finalEndDate);
but this returns
Tues Jun 11 2013 Eastern Standard Time, etc.
How do I fix this?
As far as I know, toString does not take any arguments. It's easy to construct your format though.
var finalEndDate = endDate.getFullYear() + '/' + (endDate.getMonth() + 1) + '/' + endDate.getDate();
There are several getter methods for each component of the date object to help you construct nearly any format.
I strongly encourage you to take a look at Moment.js
var str = moment(date, 'YYYY/MM/DD').add('days', 1).format('yyyy/MM/dd');
Note: moment doesn't know yyyy, what's that supposed to be? See http://momentjs.com/docs/#/displaying/format/ for supported format strings.
Related
This question already has answers here:
Format a date string in javascript
(7 answers)
Closed 2 years ago.
How do I format the date I receive from openweather api?
I currently get 2020-10-16 00:00:00 and I want 10-16-2020.
The reason I don't use moment is because I want future dates which come automatically with the 5 day forecast in the api.
You can use JavaScript's Date object.
You might save yourself time by searching a bit more before posting a question, the answer is probably already out there.
Where can I find documentation on formatting a date in JavaScript?
How to format a JavaScript date
Format JavaScript date as yyyy-mm-dd
You could try to:
Make a new date based on the date that comes from OpenWeather api
Convert it to a LocaleDateString using the 'en-US' locale. This will make the month appear before the date.
Then just split the Date String on the '/' and join in on a '-'. This will substitute the '/' with a '-'
const date = new Date("2020-10-16 00:00:00").toLocaleDateString('en-US');
const formatedDate = date.split('/').join('-');
console.log(formatedDate);
You can always use the built in Javascript Date object.
In your case you'd want to. do something like this -
const myDate = new Date('2020-10-16 00:00:00');
const date = myDate.getDate();
const month = myDate.getMonth() + 1;
const year = myDate.getFullYear();
console.log('MM-DD-YYYY', `${month}-${date}-${year}`);
If you want something more convinient and don't want to use moment you can also try some other popular date libraries. You can find some here - https://github.com/you-dont-need/You-Dont-Need-Momentjs
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 3 years ago.
I am trying to create date object without time info like 23/09/2019.
I already tried many solutions like;
new Date(d.setHours(0,0,0,0))
new Date(dateString).toUTCString().split(' ').slice(0, 4).join(' ')
new Date(d.getFullYear(), d.getMonth(), d.getDate())
new Date(new Date().getFullYear(),new Date().getMonth() , new Date().getDate())
However, all of this either give me string or date with time Tue Aug 27 2019 00:00:00 GMT+0300
I want to get this 23/09/2019 as a date object. When I write typeof result it should return date.
I know js Date object stores date and time but
Is it possible to get something like this?
Have a look at Date - Javascript | MDN for Date methods to see more details. You can compose the date components however you like.
If you want the date/month/year, you just need to call those methods:
function zeroPad(n) {
return n < 10 ? '0'+n : n;
}
var d = new Date();
var s = zeroPad(d.getDate()) + '/' + zeroPad(d.getMonth()+1) + '/' + d.getFullYear();
console.log(s);
See also getUTCDate, getUTCMonth, getUTCFullYear for UTC.
This question already has answers here:
Convert UNIX to readable date in javascript
(3 answers)
Closed 4 years ago.
JSFiddle: https://jsfiddle.net/u8w3v9fd/1/
I am trying to get the day, month and year from a date that is passed form the database in the format: DD/MM/YYYY. However I can't even seem to get the correct date to show.
Here is my code:
var time = "1522843537";
var regDateOriginal = new Date(time);
var regDate = new Date();
regDate.getMonth(regDateOriginal);
regDate.getHours(regDateOriginal);
regDate.getDate(regDateOriginal);
document.write("<p style='color: #fff'>" + regDate.getDate(regDateOriginal) + "</p>");
As you can see, this is returning:
21
Which is todays date. It should be 4
I have googled it and hacked around with various versions for the past 45 mins. I am a junior and would really appreciated a nicely commented piece of code so I can learn instead of just copying and pasting.
Thank you for your help.
From here
var time = 1522843537;
var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
d.setUTCSeconds(time);
console.log(d.getDate());
Of course, you can still do all the other Date functions as needed.
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 a function which accepts a UTC date. I want to return back the local date.
I have the following code so far:
var dateFormat = function (date) {
var dt = new Date(date + " UTC");
return dt.toString();
}
For the return date, I like it formatted as such:
October 9th, 2013, 7:34:00 PM
Is there a formatting that will handle this.
I usually use moment.js for this kind of tasks. It's pretty powerful and still small sized library. Give it a try.
You have a lot of ways to format the Date object in JavaScript. I suggest you to check out 10 ways to format time and date using JavaScript and Working with Dates. These are the main functions you need to use for formatting JavaScript Dates:
getDate(): Returns the date
getMonth(): Returns the month (Starting from 0)
getFullYear(): Returns the year
A simple script would be:
<script type="text/javascript">
var d = new Date();
var curDate = d.getDate();
var curMonth = d.getMonth() + 1; // Months are zero based
var curYear = d.getFullYear();
document.write(curDate + "-" + curMonth + "-" + curYear);
</script>
In your case, you can have an array of months:
var months = ["January", "February", ... "December"];
The next part would be associating the correct variables with the place they need to be present.
Plugins
But instead of doing all these, you can also consider using a fancy date formatter plugin, that uses jQuery or some library. A few ones would be:
Moment.js is a javascript date library for parsing, validating, manipulating, and formatting dates.
XDate is a thin wrapper around JavaScript's native Date object that provides enhanced functionality for parsing, formatting, and manipulating dates. It implements the same methods as the native Date, so it should seem very familiar.
DateJS is an open source JavaScript Date library for parsing, formatting and processing.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I output an ISO-8601 formatted string in Javascript?
I am trying to convert a date and time input to an ISO format but I am getting .toISOString is undefined? I have to be missing something silly.
var startDate = "10/11/2012";
var startTime = "12:12:00";
var fullDate = startDate + " " + startTime;
var fullDateReal = new Date(fullDate);
var iso = fullDateReal.toISOString();
Why would .toISOString() show as undefined?
I need to end up with the ISO format ("2012-10-11T12:12") timezone is optional.
Update
It looks like this problem is because IE8 does not support this. So how could I go about converting my inputs to the format listed?
Some browsers don't support ECMAScript 5 (which is required for toISOString).
http://kangax.github.com/es5-compat-table/