Date not able to Format(OOP) - javascript

The function that I am creating right now is to format the date form the console log to DDMMYYYY instead of the given format. However, I am getting this error where is says the getDate is not a function.
userDate.getDate is not a function
How should I go about solving this error?
function formatDate(userDate) {
let formatted_date = userDate.getDate() + (userDate.getMonth() + 1) + userDate.getFullYear()
return formatted_date;
}
console.log(formatDate("12/31/2014"));

You are using getDate() on a string reference, you need to convert it first to a Date object:
function formatDate(userDate) {
userDate = new Date(userDate);
let formatted_date = `${userDate.getDate()}/${(userDate.getMonth() + 1)}/${userDate.getFullYear()}`;
return formatted_date;
}
console.log(formatDate("12/31/2014"));

Related

JavaScript date conversion resulting in different values for Browser and Node js

I am using a function to convert date time into 'en-NL' format.But it gives me different result in browser and nodejs
function convertDateTime(value){
const timestamp = new Date(value);
let date = timestamp.toLocaleDateString('en-NL');
let time = timestamp.toLocaleTimeString('en-NL');
return date + ' ' + time;
}
console.log(convertDateTime(1559742499937));
when i use this function in browser it gives me following results:
05/06/2019 19:48:19
when i use this function in nodejs it gives me following result :
6/5/2019 7:48:19 PM.
But my result should be same in browser and nodejs.
The implementation of Date between browsers and node can differ a bit.
To avoid that issue, I suggest you to use a library like momentjs on both frontend and backend, afterward, you will be able to manage the format of the date and you should have the same value on both.
You can also force the format of the datetime with the following
moment().format('DD/MM/YY h:mm:ss');
If you don't want to include momentjs just for this simple function, you can always write your code to return the exact format you need
function convertDateTime(value){
const t = new Date(value);
const pad2 = n => ('0' + n).substr(-2);
let date = `${pad2(t.getDate())}/${pad2(t.getMonth()+1)}/${t.getFullYear()}`
let time = `${pad2(t.getHours())}:${pad2(t.getMinutes())}:${pad2(t.getSeconds())}`
return date + ' ' + time;
}
console.log(convertDateTime(1559742499937));

Angular 6: Invalid time value

I am trying to get two date and times as a string of numbers (epoch) so I can compare them. One is a new Date():
today = new Date().valueOf();
And one is from an api response in the format:
scheduleDate: "2019-07-22T00:00+01:00"
The issue is I am trying to get the returned date in the correct format. When I try
var scheduleDate = new Date(scheduleDate).toISOString()
console.log("converted date:" + scheduleDate);
I get the error:
Invalid time value
How do I get the returned date into epoch format?
Thanks
Your scheduleDate variable must be undefined. Are you sure it's being assigned properly?
(function() {
//number (milleseconds)
const today = new Date().valueOf();
//string
const scheduleDate = undefined; //"2019-07-22T00:00+01:00";
const scheduleDate2 = new Date(scheduleDate).toISOString()
console.log(typeof today);
console.log(typeof scheduleDate);
console.log("converted date: " + scheduleDate);
}
)();
More worryingly, why are you converting a date string to a date and then back to a string (same value).
You are missed to assign the value
scheduleDate= "2019-07-22T00:00+01:00"
Then try its working
> scheduleDate= "2019-07-22T00:00+01:00"
'2019-07-22T00:00+01:00'
> new Date(scheduleDate)
2019-07-21T23:00:00.000Z
> var scheduleDate = new Date(scheduleDate).toISOString()
undefined
> scheduleDate
'2019-07-21T23:00:00.000Z'
> console.log("converted date:" + scheduleDate);
converted date:2019-07-21T23:00:00.000Z

Moment JS add method is returning

I am using moment.js in Parse cloud code, I need to get an object by tomorrow's date. I am trying to use momentjs to get the day range.
function getTommorowSongQuery()
{
var query = new Parse.Query("Song");
var tommorow = moment().add(1, 'days');
var start = moment(tommorow).startOf('day');
var end = moment(tommorow).endOf('day');
console.log('start = ' + start.toDate() + ' end = ' + end.toDate());
query.greaterThanOrEqualTo("pushDate", start.toDate());
query.lessThan("pushDate", end.toDate());
return query;
}
The problem is that I get the current date (Today) while I expect to get tomorrow.
Any Idea what am I doing wrong.

Javascript to format date value

I am trying (and failing!) to format my date output using javascript, so I am asking for some help.
The date format is 2013-12-31 (yyyy-mm-dd), but I want to display the date as 12-2013 (mm-yyyy).
I have read many posts, but I am now just confused.
Here is the call to the javascript function:
changeDateFormat($('#id_award_grant_date').val()),
Here is the javascript function:
function changeDateFormat(value_x){
}
What you have is just a string, so just split it and put it back together in the wanted format
var date = '2013-12-31',
parts = date.split('-'),
new_date = parts[1]+'-'+parts[0];
FIDDLE
var d = '2013-12-31'
var yyyymmdd = d.split('-')
var mmyyyy = yyyymmdd[1]+'-'+yyyymmdd[0]; // "12-2013"
function changeDateFormat(str) {
var reg = /(\d+)-(\d+)-(\d+)/;
var match = str.match(reg);
return match[2] + "-" + match[1];
}
http://jsfiddle.net/wZrYD/
You can do this pretty easily with split and join.
var yyyymmdd = '2013-12-31'.split('-'),
mmyyyy = [ yyyymmdd[1], yyyymmdd[0] ].join('-');
console.log('The date is: ', mmyyyy );

Cannot find function getMonth in Object 05/06/11

Why do i get this error, when i format my date? Code follows below
var date = /Date(1306348200000)/
function dateToString(date) {
return (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getYear();
}
function dateFromString(str) {
return new Date(str);
}
You define var date as a regular which can not be accepted by new Date,just do it like this.
var date = 1312711261103;
try it like this: http://jsfiddle.net/zhiyelee/wLNSS/
In your code, date is a regular expression, not a Date object. You probably want:
var date = new Date(1306348200000);
Also note that calling date Date without new returns a string and not a Date object.
Edit: Apparently I overlooked the dateFromString function, but your code does not show what you do with date and how you use these functions. Anyway, it should be clear which value you have to pass to Date. Definitely not a regular expression.

Categories

Resources