.js Date String YYYY-MM-DD-HH-MM [duplicate] - javascript

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 4 years ago.
I am trying to get a string with date/time on this format: "YYYY-MM-DD-HH-MM"
I cannot find a proper way to do it?
This is what I have at the moment:
var x = new Date();
var TimeStamp = x.toString();
Outputs:
Tue Oct 30 2018 14:33:03 GMT+0000 (UTC)

Use Date methods and format it.
var date = new Date();
var output = date.getFullYear()+'-'+("0" + (date.getMonth() + 1)).slice(-2)+'-'+("0" + date.getDate()).slice(-2)+'-'+date.getHours()+'-'+date.getMinutes()
console.log(output)
Or you can use momentjs format.
console.log(moment().format('YYYY-MM-DD-HH-mm'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>

Related

Convert Date string to Object with current timezone format [duplicate]

This question already has answers here:
How to convert dd/mm/yyyy string into JavaScript Date object? [duplicate]
(10 answers)
Closed 2 years ago.
I have this code to add a reminder to a calendar
function addCalendarEvent(eventDate, eventTitle){
let dateObj = new Date(eventDate);
let calendarId = "me#gmail.com";
let cal = CalendarApp.getCalendarById(calendarId);
let event = cal.createAllDayEvent(eventTitle, dateObj)
event.addEmailReminder(420)
}
The eventDate is passed into the function as a string in the format dd/MM/YYYY but the output from let dateObj = new Date(eventDate); is in american format. ie. 02/10/2020 comes out as Mon Feb 10 2020 00:00:00 GMT+0000 (Greenwich Mean Time). Any help correcting this would be great. Dates give me an absolute headache
In the end I decided that just working with date objects from the off would be easiest
This will give you the date object of the string in the format dd/MM/YYYY
function ddmmyyyytFormatterWithTime(date) {
var dateDiv = date.split('/');
var date = new Date();
date.setDate(dateDiv[0]);
date.setMonth(--dateDiv[1]); // -- since it starts from 0
date.setFullYear(dateDiv[2]--);
return date;
};
function ddmmyyyytFormatter(date) {
var dateDiv = date.split('/');
var date = new Date();
date.setHours(0, 0, 0, 0); // to reset time
date.setDate(dateDiv[0]);
date.setMonth(--dateDiv[1]); // -- since it starts from 0
date.setFullYear(dateDiv[2]);
return date;
};
var date = ddmmyyyytFormatter("10/02/2020");
var dateWithTime = ddmmyyyytFormatterWithTime("10/02/2020");

new Date() always read dd/mm/yyyy as mm/dd/yyyy(month first) hence day and month values get mixed resulting in NaN/Error [duplicate]

This question already has answers here:
How to convert dd/mm/yyyy string into JavaScript Date object? [duplicate]
(10 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
When converting date for example:
var dateObj = new Date("10/01/2019");
console.log(dateObj);
returns Tue Oct 01 2019 00:00:00 i.e. it takes in the day as month and likewise with the month value
How to make new Date() to take dd/mm/yyyy ??
Answer is here: https://stackoverflow.com/a/33299764/6664779
(From original answer) We can use split function and then join up the parts to create a new date object:
var dateString = "23/10/2019"; // Oct 23
var dateParts = dateString.split("/");
// month is 0-based, that's why we need dataParts[1] - 1
var dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]);
document.body.innerHTML = dateObject.toString();

Javascript convert date string to object [duplicate]

This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 4 years ago.
I'm trying to convert a date in string format to a Date object in JavaScript. The date is coming from the openweathermap.org API, which gives the date string in the following format:
'2018-04-28 09:00:00' or 'yyyy-mm-dd hh:mm:ss'
I know that the JavaScript Date class can take other string formats of dates, but this one returns NaN when I try anything with it. How can I convert a date string, like the one above, and convert it easily to a JavaScript object? Thanks!
Since you are getting NaN while directly converting the string to date. You can split the string on spaces, - and : and then pass the value to date constructor and generate the date object.
const str = `2018-04-28 09:00:00`;
const [date, time] = str.split(' ');
const [year, month, day] = date.split('-');
const [hh, mm, sec] = time.split(':');
const dateObj = new Date(year, month - 1, day, hh, mm, sec);
console.log(dateObj);
As pointed out by #RobG, this could also be done using the regex.
const str = `2018-04-28 09:00:00`;
var b = str.split(/\D/);
var date = new Date(b[0],b[1]-1,b[2],b[3],b[4],b[5]);
console.log(date);
const str = `2018-04-28 09:00:00`,
date = new Date(...(str.split(/\D/).map((v,i)=>i==1?--v:v)));
console.log(date);
Just try new Date(str)
d = new Date("2018-04-20 09:00:00")
Fri Apr 20 2018 09:00:00 GMT+0800 (Hong Kong Standard Time)
d.getDate()
20
ref: https://www.ecma-international.org/ecma-262/6.0/#sec-date-time-string-format

Add 30 days to a Current date - JS [duplicate]

This question already has answers here:
How to add days to Date?
(56 answers)
Closed 5 years ago.
I want to add 30 days to current date and get in a format.
Im confused to add a 30 to a date and get new.
Its pure JS Solution needed.
Format : June 10, 2017
var date = new Date(); // Now
date.setDate(date.getDate() + 30); // Set now + 30 days as the new date
console.log(date);
Im assuming you are using datejs?
var newDate = Date.today().add(30).days();
try following:
var yourDate = new Date();
var numberOfDaysToAdd = 30;
console.log(yourDate.setDate(yourDate.getDate() + numberOfDaysToAdd));

How can remove time from Date string [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 8 years ago.
var x = new Date();
myVar = x.toString();
document.write(myVar);
// Sat Feb 14 2015 14:20:58 GMT+0100 (CET)
I want to remove the time <<14:20:58 GMT+0100 (CET)>>
Try ...
var x = new Date()
var myVar = x.toDateString();
This will only provide the date ...
Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString

Categories

Resources