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 .
Related
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 have an issue regarding separate year from date object. I just came to know whenever I pass the String to the DATE object then my years is incremented by 1 or 2 .
for example.
var licenceStartDateConv = new Date(licenceStartDate);
var licenceStartDateYear = licenceStartDateConv.getUTCFullYear();
when i pass the licenceStartDate = "13/08/2011" then the function returns me licenceStartDateYear = Sun Jan 8 00:00:00 UTC+0500 2012 instead of 2011. why is it so?
can someone please sort out this issue ? I shall be very grateful to you people.
Thanks in Advance.
If you are using a "xx/yy/zzzz" format to supply a date string to the Date constructor, it will be parsed as "mm/dd/yyyy", as Ankit pointed out in the original question. You will have to parse the date string to pull the Day, Month and Year out and create a Date object using these.
For example:
// Parse the date using a regular expression.
var dateFields = /(\d\d)\/(\d\d)\/(\d{4})/.exec("13/08/2011");
licenceStartDateConv = new Date(dateFields[3], dateFields[2]-1, dateFields[1]); // -1 on month to account for offset.
I got from json with date, date format is "2011-03-13T11:30:00Z" , and I want to convert it into normal format .
var Date= "Sunday, March 13th, 2011 " and var Time = "11:30"
i want to make it separate as above with proper format.
please help me....
Create a new Date object with the date string from the json data and then use the objects methods to get the date formats you want
var dateObject = new Date("2011-03-13T11:30:00Z");
var time = dateObject.getHours() + ':' + dateObject.getMinutes();
You also have the following which you could use to construct your date
dateObject.getDay(); // would return 0 for Sunday (days run 0-6 starting at Sun)
dateObject.getMonth(); // would return 2 for March (months run 0-11)
dateObject.getFullYear(); // return 2011
As per comments, to correct this for timezones, you need to know that the Z in your string denotes UTC/GMT, so if you are not in that timezone you need to correct for your difference to UTC
For example, replace the Z with +05:30 for being 5.5 hours ahead of UTC
var dateString = "2011-03-13T11:30:00Z".replace('Z', '+05:30');
var dateObject = new Date(dateString);
Let's say that I have date as a string, like: 02-12-2011
How can I parse it, and make it in format:
Friday, 02 December, 2011.
Thank you in advance!
Something like this should work:
var date = "02-12-2011".split('-');
var month = (date[0] * 1 ) - 1; // * 1 to convert to Number - 1 to subtract to 1 (months are from 0 - 11)
var day = date[1];
var year = data[2];
var d = new Date();
d.setMonth(month);
d.setDate(day);
d.setFullYear(year);
console.log(d.toDateString()); // will output Sat Feb 12 2011
You could also format the date differently by creating your own function that uses the getters getMonth(), getDate(), getFullYear(), getDay().
If you'd like a lighter weight solution. Otherwise the link that #diEcho mentions looks good.
Also, the W3School references, while not the best for style, are pretty decent for a general 'get to the facts' reference about the various JavaScript Objects.
Here's a link to the Date object: http://www.w3schools.com/jsref/jsref_obj_date.asp
This blog article on JavaScript date formatting will help on the formatting part.
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());