Get month number from a date in javascript - javascript

I have a daterangepicker function that is returning the selected date in this format 06 May 2016. What I am trying to do is extract the month as an integer, therefore from the above I should be able to return the number 5.
This is the line of code that returns the selected date - getDateString(new Date(opt.start)
Any help is appreciated thanks.

var date = new Date();
var month = date.getMonth();
The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.

var datestring = getDateString(new Date(opt.start);
var monthNumber = new Date(datestring).getMonth()+1;

I'm not sure this will help since I am new in this field
var today = new Date();
var month = new Array();
month[0] = "01";
month[1] = "02";
month[2] = "03";
month[3] = "04";
month[4] = "05";
month[5] = "06";
month[6] = "07";
month[7] = "08";
month[8] = "09";
month[9] = "10";
month[10] = "11";
month[11] = "12";
var monthNumber = month[today.getMonth()];
console.log("This month is " + monthNumber);

Thanks, and more simple:
var month = new Date().getMonth() + 1;
// Result, ie: January = 1 ... December = 12
var month = new Date().getMonth() + 1;
console.log("month", month);

Related

display current month until the 15th day, then to next month after the 15th day. E.g: If July 10 - display “July”, If July 20 - display “August”,

I've written script for both current month & following month (shown below). However, I am trying to achieve something in the middle; displaying the current month until the 15th day, then changing to next month after the 15th day.
Example:
If today is July 10th, display “July”.
If today is July 20th, display “August”.
Displaying the Current Month, alternating: “var n = month[d.getMonth()];”
{
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var d = new Date();
var n = month[d.getMonth()];
document.write (month = n)
}
Displaying the Following Month, alternating: “var n = month[d.getMonth()+1];”
{
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var d = new Date();
var n = month[d.getMonth()+1];
document.write (month = n)
}
I've experimented with values in between the 0-1, but still no luck. I haven't managed to find a solution after a lot of research so any help would be much appreciated.
Thanks in advance,
Ryan.
You can get the day of the month with new Date().getDate(). Then, if it is more than 15, add 1 to the month index to return.
Also, I rewrote your months array in a simple form instead of month[0] = "January";
{
var month = ["January","February","March","April","May","June","July","August","September","October","November","December"]
var d = new Date();
var dayOfMonth = d.getDate();
var n = month[ d.getMonth() + (dayOfMonth > 15 ? 1 : 0) ];
document.write ("month = " + n)
}
We can create a function to return the month name from the month, and a function to get the next month.
Then, if the current day is 15 or greater, we'll show the next month:
function getMonthName(month, locales = 'default') {
return new Date(new Date().getFullYear(), month, 1).toLocaleString(locales, { month: 'long'} );
}
function getNextMonth(month) {
return (month + 1) % 12;
}
function getMonthToDisplay(date) {
let month = date.getMonth();
if (date.getDate() >= 15) {
month = getNextMonth(month);
}
return getMonthName(month);
}
let dates = ["2021-06-14T18:00:00Z","2021-06-15T00:00:00Z", "2021-09-30T00:00:00Z", "1965-11-01T15:00:00Z"].map(dt => new Date(dt));
for(let d of dates) {
console.log("Date:", d.toDateString(), "Current:", getMonthName(d.getMonth()), "Display:", getMonthToDisplay(d))
}
console.log("Testing all days in June 2021...");
dates = [...Array(30).keys()].map((v,k) => new Date(2021, 5, k + 1));
for(let d of dates) {
console.log("Date:", d.toDateString(), "Current:", getMonthName(d.getMonth()), "Display:", getMonthToDisplay(d))
}
You can get
date -> new Date().getDate()
month -> new Date().getMonth()
and compare date with 15 and get the desired result.
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
const now = new Date();
const mon = now.getMonth();
const date = now.getDate();
const result = date > 15 ? month[mon + 1] : month[mon];
console.log(result);
document.write(result)

Javascript Date +15 Days

I want this day to be 25 days from now, and it needs to correspond with normal workday measurements. Saturdays, Sundays and Holidays need to be removed. I've tried using Google sheets and javascript:
Here is what I have.
function Calendar() {
var now = new Date();
var year = now.getFullYear();
var month = new Array();
month[0] = "01";
month[1] = "02";
month[2] = "03";
month[3] = "04";
month[4] = "05";
month[5] = "06";
month[6] = "07";
month[7] = "08";
month[8] = "09";
month[9] = "10";
month[10] = "11";
month[11] = "12";
var monthnum = month[now.getMonth()];
var weekday = new Array();
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var day = weekday[now.getDay()];
var date = now.getDate();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Schedule");
var last = sheet.getLastRow();
var destination = sheet.getRange(last, 1);
var format = sheet.getRange(10, 1, 1, 7);
var insertdate = sheet.getRange(last, 2);
var insertday = sheet.getRange(last, 1)
sheet.insertRowAfter(last);
sheet.deleteRow(7)
insertdate.setValue(monthnum + "/" + date + "/" + year);
insertday.setValue(day)
Logger.log(monthnum + "/" + date + "/" + year + " " + day)
}
There are many ways to do it, but the easily one is: 86400000 is one day
86400000= 24 Hours * 60 Min * 60 Seconds * 1000 Milliseconds
Here is an example which may help you:
var first = new Date();
var last = new Date(new Date(first).getTime() + (86400000 * 25));
var now = new Date(); now.setDate(now.getDate() + 25);
console.log(first);
console.log(last);
console.log(now);

JavaScript currant date month format issue [duplicate]

This question already has answers here:
Get month name from Date
(40 answers)
Closed 5 years ago.
Im try to do using html convert to full name of month but i cant correctly do that , anyone know how to do that i need to 21-September-2017
please help me to fix this
thanks,
function init(){
var d = new Date();
var day = d.getDate();
var x = d.toDateString().substr(4, 3);
var year = d.getFullYear();
document.querySelector("#date").innerHTML = day + '-' + x + '-' + year;
}
window.onload = init;
<div id="date"></div>
You can use toLocaleString() to get month.
function init(){
var d = new Date();
var day = d.getDate();
var locale = "en-us";
var month = d.toLocaleString(locale, { month: "long" });
var year = d.getFullYear();
document.querySelector("#date").innerHTML = day + '-' + month + '-' + year;
}
window.onload = init;
<div id="date"></div>
You can not get full month name directly. But you can do a manual implementation like this,
var d = new Date();
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var n1 = month[d.getMonth()];
var day = d.getDate();
var year = d.getFullYear();
var final_date = day+"-"+n1+"-"+year;
FIDDLE
Use Date.prototype.toLocaleString
It converts your date to the locale format used by the language and country code specified. Only usable in up to date browsers.
function init(){
var d = new Date();
var options = {day: 'numeric', month: 'long', year: 'numeric' };
document.querySelector("#date").innerHTML = d.toLocaleString('en-US', options);
}
window.onload = init;
<div id="date"></div>
This will use the current time zone of the client. To negate this use these options:
options.timeZone = 'UTC';
options.timeZoneName = 'short';

Instagram API created_time to date with Moment.js

I want to parse the string JSON object of created_time from the Instagram API into a readable date. I've read several posts saying that new Date("1923111") (whatever created_time is) will solve this issue, but I'm getting back an invalid date error.
I want to use Moment.js to parse it as such:
moment.unix(created_time).format("MMMM D, YYYY")
but I'm receiving 1469576855000-20en as an output. Is there any other way to do this?
var date = new Date(image.created_time*1000);
m = date.getMonth();
d = date.getDate();
y = date.getFullYear();
var month_names = new Array ( );
month_names[month_names.length] = "01";
month_names[month_names.length] = "02";
month_names[month_names.length] = "03";
month_names[month_names.length] = "04";
month_names[month_names.length] = "05";
month_names[month_names.length] = "06";
month_names[month_names.length] = "07";
month_names[month_names.length] = "08";
month_names[month_names.length] = "09";
month_names[month_names.length] = "10";
month_names[month_names.length] = "11";
month_names[month_names.length] = "12";
var thetime = y + '-' + month_names[m] + '-' + d;
image.created_time = thetime;
return true;
var date = new Date(parseInt(created_time) * 1000);
var month = (date.getMonth()+1);
var date = date.getDate();
var fullYear = date.getFullYear();

My highlighted current date via javascript is not showing correctly

I have successfully managed (with the help from stack overflow) to loop through a calendar starting from may until end oktober 2015. now i want to highlight the current date which is 28 june sunday. but for some reason it highlights the wrong date and i don't think its because of the code but i am not a 100% sure.
checkout this link to see it visually (http://gyazo.com/30e448d0f84c8f1c55e4dd1ce6d91f38)
jsfiddle link: https://jsfiddle.net/GY22/vqfk8yLL/
this is my html code:
<ul id="timeline">
<li></li>
</ul>
this is my javascript code:
<script>
// Get today's current date.
var now = new Date();
console.log(now);
// Calculates the number of the week
var weekNumber = ((now.getDate()<10) ? "0" : "")+ now.getDate();
console.log("The current week number is: " + weekNumber);
// Array list of months.
var month = new Array(12);
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
//console.log(month[3]);
var weekDay = new Array(7);
weekDay[0]= "Su";
weekDay[1] = "Mo";
weekDay[2] = "Tu";
weekDay[3] = "We";
weekDay[4] = "Th";
weekDay[5] = "Fr";
weekDay[6] = "Sa";
function formatDate(date) {
var month = date.getUTCMonth() +1;
var dayNumber = date.getUTCDate();
var year = date.getUTCFullYear();
var day = date.getUTCDay();
return weekDay[day] + ": " + dayNumber + "-" + month + "-" + year + "; ";
//return weekDay[day] + " " + dayNumber + "; ";
}
//console.log(formatDate(new Date()));
var today
function addListItem(){
var createListItem = document.createElement("li");
var outputListItem = document.createTextNode(today);
createListItem.appendChild(outputListItem);
var createUl = document.getElementsByTagName("ul");
createUl[0].appendChild(createListItem);
}
// loop starting from may up untill for months from the set date
for (var i = 0; i < 122; i++){
today = formatDate(new Date(2015, 05, i));
//document.write(today + "<br />");
addListItem();
}
document.getElementById('timeline').
getElementsByTagName('li')[(new Date()).getDate() - 1].className += ' currentDate';
Change the last line from:
getElementsByTagName('li')[(new Date()).getDate() - 1].className += ' currentDate';
To:
getElementsByTagName('li')[(new Date()).getDate() + 1].className += ' currentDate';
The date appeared to be off by just one day before, so that should fix it.
Consider using MomentJS instead. It would save you a lot of code and date problems:
http://momentjs.com/
If you prefer to keep your code, move your current date logic inside your addListItem() method.
if(today == formatDate(new Date())) {
createListItem.className += ' currentDate';
}

Categories

Resources