I would like to a way to get the current date in Javascript.
I would also like the date to be displayed in this format:
dd month year # hh:mm:ss EDT
for today's date that would work out to:
04 Aug 2011 # 20:24:38 EDT
Thanks
var d=new Date();
d.toUTCString();
Is this what you need?
You can easily build it yourself with the Date object's various properties. To get a Date object representing the current moment in time, use
var now = new Date();
Go grab date.js. It can do what you want.
new Date();
will give you the current date.
let currentDate = (date = new Date()) => `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`
console.log(currentDate());
Related
How can I find the beginning of the year for a set date? That is, let's say I set the date 2018-07-28, then the result should be 2018-01-01. I know that for example in Rails there is such a function as beginning_of_year.
Is there anything similar for nodejs?
Or maybe someone already has a similar for javascript?
You could use getFullYear and construct a new Date object
function beginning_of_year(date) {
return new Date(date.getFullYear(), 0);
}
beginning_of_year(new Date()); // Wed Jan 01 2020 00:00:00
First, Get the year from date and concatenation of string "01-01". Please check below :
new Date('2018-07-28').getFullYear()+'-01-01'
But if you wants to create Date object for year begging day. Please check below :
new Date(new Date('2018-07-28').getFullYear(), 0, 1)
You can do with the help of moment library .
var thisYear = (new Date()).getFullYear();
var start = new Date("1/1/" + thisYear);
var defaultStart = moment(start.valueOf());
It is easy way and also a efficient way .
I just came across this format of start and end in the codebase which is probably the starting and ending date and time.However, I could not figure out this.
I know how to generate date and time in javascript but I need to generate it in this format to be able to fetch data from API.
Here is the format.
start : '2018-06-20T11:44:21.938Z',
end : '2018-07-20T11:44:21.938Z',
At the same time, I would like to know how to get date and time with exact this format?
It is ISO format of the date. You can use toISOString() method to achieve it:
// current date
var date = new Date();
console.log(date);
console.log(date.toISOString());
let date = new Date( Date.parse('2018-06-20T11:44:21.938Z'));
To retrieve date & time separately.
date.toLocaleDateString();
date.toLocaleTimeString()'
To convert any date object to your desired format (ISO Dates):
var date = new Date();
date.toISOString();
var date = new Date(Date.parse('2018-06-20T11:44:21.938Z'));
It's ISO 8601 format.
Check this:
https://en.wikipedia.org/wiki/ISO_8601
Have a look at toISOString() in MDN
var event = new Date('05 October 2011 14:48 UTC');
console.log(event.toString());
// expected output: Wed Oct 05 2011 16:48:00 GMT+0200 (CEST)
// (note: your timezone may vary)
console.log(event.toISOString());
// expected output: 2011-10-05T14:48:00.000Z
That is the ISO date format.
similar to this:
How do I output an ISO 8601 formatted string in JavaScript?
Here is code to produce it.
function myFunction() {
var d = new Date();
var n = d.toISOString();
document.getElementById("demo").innerHTML = n;
}
<button onclick="myFunction()">convert to ISO date format</button>
<p id="demo"></p>
It is an ISO date. Check this link for some info:
https://www.w3schools.com/js/js_date_formats.asp
var istDate = new Date(); // will return: Wed Sep 05 2018 17:50:39 GMT+0530 (India Standard Time)
var isoDate = istDate.toISOString(); // will return in ISO format i.e. "2018-09-05T12:20:39.732Z"
var dateString = istDate.toDateString(); // will return in format: "Wed Sep 05 2018"
Browser console will provide suggestions for various methods of Date Object for extracting day, month, year, etc from the new Date()
In my project, i use js code below to take date with date input only year and month:
var current_time = new Date(current);
with current data is something like this: "2017/04"
It work perfect on chrome but on IE 11, i get invalid date for current_time. Can your guys help to to take date form data which only has year and month like this on IE? Thankyou.
Dates should be formatted according RFC 2822 or ISO 8601 So if you use '-' instead of '/ it will work every where.
console.log(new Date("2017-04"))
if you want to still have your date with '/' you can do this
console.log(new Date("2017/04".replace(/\//g, "-")));
The format you are using is not a standard format of date. Non standard date formats cause problems on various browsers like IE, safari, etc.
Use this method. It will work on all IE as well
Run it on chrome and IE. Here in this snippet, it will give one month less since stack snippet is using a different library for date parsing.
var input = "2017/04"
var year = input.split("/")[0]
// indexing starts with 0
var month = parseInt(input.split("/")[1],10) -1
var date = new Date(year,month)
console.log(date)
This is what it will output on browsers including IE
Sat Apr 01 2017 00:00:00 GMT+0530 (India Standard Time)
In order to create the date by passing the month and year to dateObject you can do:
current = '2017/04';
current = current.split("/")
var date = new Date(current[0], current[1]);
// it will return the date object where date is set as the first day of the month
console.log(date)
which will give you the date object set to the first day of the month.
If you want to get the current month and year you can do:
var year = new Date().getFullYear();
var month = new Date().getMonth() + 1;
date = year + '/' + month;
console.log(date);
I'm trying to convert today's date to an ISO standard string but with the fixed time of T00:00:00.000Z.
I can get as far as returning a ISO string of today's date and time:
var isoDate = new Date().toISOString();
// returns "2015-10-27T22:36:19.704Z"
But I wanted to know if it's possible to have a fixed time, so it should return:
"2015-10-27T00:00:00.000Z"
Is this possible?
Any help is appreciated. Thanks in advance!
To get the current UTC date at midnight:
var d = new Date();
d.setUTCHours(0);
d.setUTCMinutes(0);
d.setUTCSeconds(0);
d.setUTCMilliseconds(0);
var output = d.toISOString();
To get the current local date, with the time portion set to UTC midnight:
var d = new Date();
var ts = Date.UTC(d.getFullYear(), d.getMonth(), d.getDate());
var output = new Date(ts).toISOString();
As for which to use, think through your requirements very carefully, The current UTC date and the local date may indeed be two different days.
For example, when it's midnight (00:00) October 27th in UTC, it's 8:00 PM on October 26th in New York.
Also, consider using moment.js, which makes operations like either of these much easier with the startOf('day') and .utc() functions.
In my web-site I am getting a date from user which in format like "dd-mm-yyyy",now I want to get the date which is 7-day before of that user's date.
I am able to get the current date in format "dd-mm-yyyy" but how would I know the date which is one week before user's date in javascript?
If you already have a Date object, use yourDate.setDate(yourDate.getDate() - 7 );
Try this--
var MyDate = new Date('11/30/2012'); //date format in mm/dd/yyyy
MyDate.setDate(MyDate.getDate() -7)
var newDate = MyDate.getMonth()+1 + '/' + MyDate.getDate() + '/' + MyDate.getFullYear()
alert(newDate);
Note- subtracting seven days to a date shifts the month or year and the changes are handled automatically by the Date object.
Why don't you use datejs, it's the best date related js library I have seen. Chcek the documentation here.
http://code.google.com/p/datejs/wiki/APIDocumentation
Search for add method
Set Dates
We can easily manipulate the date by using the methods available for the Date object.
In the example below we set a Date object to a specific date (14th January 2010):
var myDate=new Date();
myDate.setFullYear(2010,0,14);
And in the following example we set a Date object to be 7 days in past:
var myDate=new Date(); //or users date
// myDate will be users current date
myDate.setDate(myDate.getDate()-7);
//now substract 7 days to get the date u want.
Follow the following link
http://www.w3schools.com/js/js_obj_date.asp
Or follow this
http://www.techrepublic.com/article/manipulate-time-and-date-values-with-javascripts-date-object/6076869
You can convert a date string in the format dd-mm-yyyy to a date object using:
function toDate(d) {
d = d.split('-');
return new Date(d[2], --d[1], d[0]);
}
Then use Osiris' answer to add or subtract 7 days.