How to convert date into specific date format [duplicate] - javascript

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
How can I convert string to datetime with format specification in JavaScript?
(15 answers)
Closed 3 years ago.
I am having date which is in mm/yyyy format. I need to compare this date with today's date. I have converted today's date.
After converting I have got date in mm/dd/yyyy format..But I need to covert it into mm/yyyy format..So that I can compare this date into, date which I am getting..Any help please.
selecteddate=05/2019 //which is in mm/yyyy format
myDate= new Date().toLocaleDateString(); //which is in mm/dd/yyyy format( I need to convert this date into mm/yyyy format and need to compare with selecteddate)

Using toLocaleDateString options
let date = new Date().toLocaleDateString('en-US', {year: 'numeric', month: '2-digit' })
console.log(date)

Hope this helps,
const date = new Date();
const myDate = `${(date.getMonth() + 1)}/${date.getFullYear()}`;
console.log(myDate);

You can use Moment.js, as follows:
moment().format('MM/YYYY')

import datetime
selecteddate='05/2019'
selecteddate= datetime.datetime.strptime(selecteddate, '%m/%Y')
today_dtime= datetime.datetime.now()
today_dtime.date()>selecteddate.date()
output:True
One thing note down month and year will assign as in selecteddate,But date will be 1st of respected month.
try this simple trick

You can use the following logic to compare your selected date with today's date
const selectedDate="05/2019"; // MM/YYYY
const today = new Date();
const todayString = `${(today.getMonth() + 1)}/${today.getFullYear()}`;
const matched = ( todayString === todayString );
console.log("Does your selected date matched with today's date ?",(matched ? "Matched":"Don't Matched"));

Related

mistake to convert date time in angular with moment library in angular

When I want to convert the Gregorian date to Persian date, it converts the value of the minute in the date conversion to error.
For example I want to convert this date time to Persian date:
2020-09-14T16:51:00+04:30 must convert to this 1399/06/24 16:51 but when convert date it show me this time 1399/06/24 00:06 it mistake to convert 16:51, show this: 00:06.
This is my code to convert date:
toPersianDate(date: any, format = 'YYYY/MM/DD HH:MM'): string {
let dateTime;
const MomentDate = moment(date, 'YYYY/MM/DD');
dateTime = MomentDate.locale('fa').format('jYYYY/jMM/jDD HH:jMM');
return dateTime;
}
What's the problem? How can I solve this problem?
the MM is used for month formatting, so it is trying to format the minutes into a month.
What you need to use is the small mm. Moreover, I don't this you need the j before the mm as the minutes are the same in Jalali time.
So what you actually need is this: MomentDate.locale('fa').format('jYYYY/jMM/jDD HH:mm');
You can read more about the formatting here.

Invalid Date for some UTC Strings Javascript [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
Trying to get UTC day of the week for any given timestamp on any given machine (w/ their own local time) I used:
var date = new Date(timestamp).toLocaleString('en-GB', { timeZone: 'UTC' });
Once I try to convert the date string to UTC date I get Invalid Date for some dates... it all seems pretty weird.
$ node
> date = new Date('15/08/2019, 00:00:00');
Invalid Date
> date = new Date('12/08/2019, 00:00:00');
2019-12-08T00:00:00.000Z
> date = new Date('15/08/2019');
Any idea where the Invalid Date issue may come from?
By converting the timestamps to strings using the "en-GB" locale, it looks like you're getting them in DD/MM/YYYY format. But in your second example, the strings are being interpreted as "MM/DD/YYYY" in whatever your default locale is, so the first call fails because 15 isn't a valid month number.

How can I convert this date format? [duplicate]

This question already has answers here:
Convert date to specific format in javascript?
(4 answers)
Closed 5 years ago.
An API I use provides a date in the object like 2018-02-14T17:00:00. How can I convert this to make it say: Tuesday, February 14th 7:00 pm
I know how to use .getMonth() methods on a date object but is it possible to do something similar with a string in a date format like this in Javascript?
You can use momentjs to format the date object.
console.log(new moment('2018-02-14T17:00:00').format('dddd, MMMM Do h:mm a'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>
You can parse the string into separated values first using String.split() method.
let rawDate = '2018-02-14T17:00:00';
let date = rawDate.split("T")[0]; //2018-02-14
let time = rawDate.split("T")[1]; //17:00:00
let year = date.split("-")[0],
month = date.split("-")[1],
day = date.split("-")[2];
let hr = time.split(":")[0],
mm = time.split(":")[1],
ss = time.split(":")[2];
Now just format these separated values using new Date(year, month, day) etc.
try this
console.log(new moment('2018-02-14T17:00:00').format('LLLL'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>
JavaScript natively does not provide a way for you to stringify a date with a provided format.
To do that, you can use moment.js. Here's the specific documentation that says how to do that:
https://momentjs.com/docs/#/displaying/format/

How to create date from locale value string? [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 5 years ago.
How do I create a date from a non-English language?
When using an English formatted string, the date returns a valid date.
var value = "3 mei, 2017 - 11:36";
//var format = 'd MMMM, yyyy - hh:mm';
var d = new Date(value);
When using a non-English formatted string, the date returns an invalid date.
var value = "3 may, 2017 - 11:36";
//var format = 'd MMMM, yyyy - hh:mm';
var d = new Date(value);
What should I change to my code to make the second version run?
I prefer you to use moment.js library.
https://momentjs.com/

JavaScript date objects UK dates

I have the following code
datePicker.change(function(){
dateSet = datePicker.val();
dateMinimum = dateChange();
dateSetD = new Date(dateSet);
dateMinimumD = new Date(dateMinimum);
if(dateSetD<dateMinimumD){
datePicker.val(dateMinimum);
alert('You can not amend down due dates');
}
})
dateSet = "01/07/2010"
dateMinimum = "23/7/2010"
Both are UK format. When the date objects are compared dateSetD should be less than dateMinimumD but it is not. I think it is to do with the facts I am using UK dates dd/mm/yyyy. What would I need to change to get this working?
The JavaScript Date constructor doesn't parse strings in that form (whether in UK or U.S. format). See the spec for details, but you can construct the dates part by part:
new Date(year, month, day);
MomentJS might be useful for dealing with dates flexibly. (This answer previously linked to this lib, but it's not been maintained in a long time.)
This is how I ended up doing it:
var lastRunDateString ='05/04/2012'; \\5th april 2012
var lastRunDate = new Date(lastRunDateString.split('/')[2], lastRunDateString.split('/')[1] - 1, lastRunDateString.split('/')[0]);
Note the month indexing is from 0-11.
var dateString ='23/06/2015';
var splitDate = dateString.split('/');
var month = splitDate[1] - 1; //Javascript months are 0-11
var date = new Date(splitDate[2], month, splitDate[0]);
Split the date into day, month, year parts using dateSet.split('/')
Pass these parts in the right order to the Date constructor.
Yes, there is problem with the date format you are using. If you are not setting a date format the default date that is used is 'mm/dd/yy. So you should set your preferred date formate when you create it as following when you create the date picker:
$(".selector" ).datepicker({ dateFormat: 'dd/mm/yyyy' });
or you can set it later as:
$.datepicker.formatDate('dd/mm/yyyy');
When you try to create a date object:
new Date(year, month, day, hours, minutes, seconds, milliseconds)
Example:
dateSetD = new Date(dateSet.year, dateSet.month, dateSet.day);
Note: JavaScript Date object's month starts with 00, so you need to adjust your dateset accordingly.

Categories

Resources