I have a date in the format yyyy-MM-dd HH:mm:ss that I am using to create moments for x axes values in chart.js
I get invalid date when I am newing up an instance of the moment.js class
$.each(unique, function (index, value) {
var momentDate = moment(value, "DD MMM YY HH:mm:ss");
momentsArray.push(momentDate);
});
I have read the documentation and I believe I am following the right ISO format for creating moment.js object. What I am doing wrong ?
According to your date value "2020-10-19 15:50:20", your format string should be "YYYY-MM-DD HH:mm:ss"
Related
I'm trying to format the date in Google App Script and then store it into the Google Sheet. Everything is working fine but the problem is source date has no specific format, dates may come in different formats like DD-MM-YYYY, MM-DD-YYYY, YYYY-MM-DD or date can also come with time and then I've to convert it into YYYY-MM-DD format and then save in google sheet.
I've tried to convert to covert using the below codes:
var date = new Date("01-15-2022").toISOString().split('T')[0] // MM/dd/yyyy
Logger.log(date);
this code works only if the source date has in a format like MM/dd/yyyy and yyyy/MM/dd only if the date is in a format like dd/MM/yyyy then codes does not work.
I want a method that converts all the date formats in a single format like yyyy/MM/dd
I got a little bad news for you. There is no way to distinguish algorithmically between 01-02-2022 (Feb 1, 2022) and 01-02-2022 (Jan 2, 2022). So technically this source data is the infamous 'garbage in'.
Somehow I managed to find an alternative option to achieve this using Moment Js Library below is the solution:
function myFunction() {
let formats = ["DD/MM/YYYY", "MM/DD/YYYY", "YYYY/MM/DD", "DD/MM/YYYY hh:mm:ss", "DD/MM/YYYY hh:mm:ss A", "MM/DD/YYYY hh:mm:ss A", "YYYY/MM/DD hh:mm:ss A"]
Logger.log(convertDate("5/1/2022 12:00:00 PM", formats));
}
function convertDate(date, formats){
eval(UrlFetchApp.fetch('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js').getContentText());
let dateMomentObject = moment(date, formats);
let dateObject = dateMomentObject.toDate();
let dateStr = moment(dateObject).format('YYYY-MM-DD');
return dateStr;
}
I am trying to get date from string in specific format like:
const format = 'MM/dd/yyyy HH:mm';
const dt = "2021-03-11T22:00:00.000Z"; // expecting "03/11/2021 22:00"
console.log(moment(dt).format(format)); // but getting "03/Th/yyyy 23:00"
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
I tried js date, moment, luxon and I don't understand how to make this :(
I suspect that 000Z made problem but this is date I get.
The difference between Moment.js and Luxon is the case of the format keys. Please review the key/token tables.
Moment.js / String + Format
Luxon / Table of tokens
Note: Moment.js has been deprecated in favor of the team's newer library Luxon and other ECMA Script standards that are being introduced in ECMA-402. If you are considering using Moment.js now, just switch over to Luxon.
If you are using Luxon, you can call .toUTC() right before you format the date. Make sure your day of month (dd) and year (yyyy) format keys are lower-case.
const DateTime = luxon.DateTime;
const
format = 'MM/dd/yyyy HH:mm',
dt = "2021-03-11T22:00:00.000Z";
console.log(DateTime.fromISO(dt).toUTC().toFormat(format)); // 03/11/2021 22:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/1.26.0/luxon.min.js"></script>
If you are using Moment.js, you can call .utc() right before you format the date. Make sure your day of month (DD) and year (YYYY) format keys are upper-case.
const
format = 'MM/DD/YYYY HH:mm',
dt = "2021-03-11T22:00:00.000Z";
console.log(moment(dt).utc().format(format)); // 03/11/2021 22:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Your format string is wrong, it should be: 'MM/DD/YYYY HH:mm'.
Then, if your date comes with ':000Z', you should remove it with the substring() method.
Working code:
const format = 'MM/DD/YYYY HH:mm';
const dateString = "2021-03-11T22:00:00:000Z";
const date = dateString.substring(0, dateString.length - 5);
console.log(moment(date).format(format)); // prints 03/11/2021 22:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
You need to use YYYY instead of yyyy and DD instead of dd to get the expected result.
By the way, I suggest using another library than Moment, like dayjs for instance.
I want to change the format of the date which I entered using date picker
i want to change it dd-mm-yyyy
but now it's in mm/dd/yyyy
If you have date as string, use this to convert in date and then format it
String sDate1="03/08/1993";
Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);
Use this
String pattern = "dd-MM-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(date1);
System.out.println(date);
to format date in any format
Simply use the following lines of code:
$(".datepicker").datepicker({
dateFormat: "dd-mm-yy",
});
In fact, yy serves the purpose of yyyy here.
I have date in this format
2017-01-09T18:30:00.000Z
I am using moment.js and I am trying to convert it into DD-MMM-YYY HH:mm:ss --> 09-Jan-2017 18:30:00
I have tried this method
dateTime = moment("2017-01-09T18:30:00.000Z").format("DD-MMM-YYYY HH:mm:ss");
But I got output like this 9/1/2017 0:00
What I miss?
The reason you're getting 00:00 is because moment converts the date object to your timezone. In order to remove this and format the date without the timezone, use moment.utc().
Update your fiddle to this:
var dateTime = moment.utc("2017-01-09T18:30:00.000Z").format("DD-MMM-YYYY HH:mm:ss");
document.getElementById('output').innerText = dateTime;
and it will work, outputting: 09-Jan-2017 18:30:00
Try using:
dateTime = moment("2017-01-09T18:30:00.000Z").format("DD-MMM-YYYY HH:mm:ss"); // note the extra Y in YYYY
console.log(dateTime);
// => 10-Jan-2017 05:30:00
For the most part it looks like what you are doing is right.
With $.datepicker.formatDate I can only format a date, meaning if I pass in a Date object, I can only get the date part of it formatted but not the time part.
Is there any equivalent function which allows the time part to also be formatted/parsed, preferably in the same syntax?
I'm using the following datetime plugin:
https://github.com/eternicode/bootstrap-datepicker
But it doesn't seem to have an equivalent formatDate function that I can use.
The datepicker's formatDate() method does not have any option to format time, for that look at a datetime library like moment.js
Ex
var date = new Date();
var df = moment(date).format('MMMM Do YYYY, h:mm:ss a');
alert(df)
and
var d = moment('2013-09-11 14:44:03', 'YYYY-MM-DD HH:mm:ss');
console.log(d.format('M/D/YYYY h:m A'))
Demo: Fiddle