I'm trying to present a Date inside ng-grid, I wrote a filter either the date is Expired or not, if so present a text otherwise a Date format MMM dd yyyy, hh:mm a
.filter("expiredDate", Filter);
function Filter($filter) {
return function (val) {
if (val === 'Expired')
return val;
var res = $filter('date')(val, 'MMM dd yyyy, hh:mm a');
return res;
}
}
This displays "/Date(1495366770000)/" instead of the Date and this is the value of val
Related
const formatChatDate = (chatDate) => {
// round to the nearest whole number
const dt = Moment(chatDate).format('MMMM Do YYYY, h:mm:ss a'); // March 9th 2021, 7:16:16 pm;
const dtNow = Moment(new Date()).format('MMMM Do YYYY, h:mm:ss a'); // March 9th 2021, 7:16:16 pm;
const diffDate= Math.round((dt-dtNow)/(1000*60*60*24));
if(diffDate.diff(dt,'days')<1){
return dt.format("LT");
}else{
return dt.format("LLL");
}
};
I need to calculate the difference between two dates and yesterday chat it will look like ll format and current date lt formate
I need to set 11.59pm mean ll format and 12pm mean lt format.. chatdate mean last message date and dtnow mean current date
Why don't use moment's diff to calculate difference between dates? Something like:
const formatChatDate = (chatDate) => {
const dt = moment(chatDate);
const dtNow = Moment(new Date());
if(dtNow.diff(dt,'days') < 1){
return dt.format("LT");
} else {
return dt.format("LLL");
}
};
const formatChatDate = (chatDate) => {
if(!chatDate) return '';
const dt = Moment(chatDate);
// dt.set({hour:0,minute:0,second:0,millisecond:0})
const dtNow = Moment(new Date());
if(dtNow.diff(dt, 'days') < 1) {
console.log(dt.format("LT"));
//console.log(dtNow);
return dt.format("LT");
// return chatDate;
} else {
return dt.format("LLL");
}
};
[![
march 8 2021 10.21 am and 11.12 pm same date but I got different format #Giovanni Esposito
][2]][2]
I have the following JavaSCript code:
function getDate() {
return new Date().toJSON();
}
function getUtcDate() {
var date = new Date();
var now_utc = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
return new Date(now_utc).toJSON();
}
Bothe are returning the same date time, I was expecting getDate() to return my local time.. it doesn't as you can see in the image
I uploaded a jsfiddle here.
Any suggestions as to what's going on?
function getDate() {
return new Date().toLocaleString();
}
function getDateOffset() {
return new Date().getTimezoneOffset() / 60;
}
function getUtcDate() {
return new Date().toUTCString();
}
console.log('Local Date = '+ getDate());
console.log('local TimeZone Offset = '+ getDateOffset())
console.log('UTC Date = '+ getUtcDate());
Updated
var date = moment.utc().format();
console.log(date, "UTC");
var local = moment.utc(date).local().format();// add parameter to format to remove Timezone Offset
console.log(local, "UTC to Local + TimeZone Offset");
console.log('---------------------------------------------');
// you can use .format() from momentjs to return any format you want
var formattedDate = moment.utc().format('YYYY-MM-DD HH:mm:ss a');
console.log(formattedDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
const moment = require('moment');
var format_1 = 'dddd, MMMM DD, yyyy';
var date_1 = 'Thursday, January 03, 2019';
console.log(moment(date_1, format_1).isValid());// This returns true
var format_2 = 'dddd, MMMM DD, yyyy';
var date_2 = 'Friday, May 01, 2020';
console.log(moment(date_2, format_2).isValid());// And this returns false
The first console.log() returns like true, as expected. Proving any date in the year 2020 the result of the isValid() function is false for this specific time format. Thanks for any help :)
Year is represented as YYYY as per moment.js docs
Modified your existing code to get the desirable output below :
var format_1 = 'dddd, MMMM DD, YYYY';
var date_1 = 'Thursday, January 03, 2019';
console.log(moment(date_1, format_1).isValid());// This returns true
var format_2 = 'dddd, MMMM DD, YYYY';
var date_2 = 'Friday, May 01, 2020';
console.log(moment(date_2, format_2).isValid());// And this returns true as well
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
I have a datepicker which returns local time in the format dddd, MMMM DO YYYY, h:mm:ss a to a variable localTime. I have an option to change the timezone from a <select> which contains timezone names from moment.tz.names(). When timezone changes, I need to change the timezone of localTime to new timezone without changing the time.How can I do this with moment.js
let timeString = "Wednesday, January 10th 2018, 9:10:19 pm";
let localTime = moment(timeString, 'dddd, MMMM DO YYYY, h:mm:ss a').format('YYYY-MM-DD HH:mm:ss ZZ');
let localTimeInUtc = moment(timeString, 'dddd, MMMM DO YYYY, h:mm:ss a').utc().format('YYYY-MM-DD HH:mm:ss ZZ');
let newTimezone = "EST";
let newTime = moment(timeString, 'dddd, MMMM DO YYYY, h:mm:ss a').tz(newTimezone).format('YYYY-MM-DD HH:mm:ss ZZ');
let newTimeInUtc = moment(timeString, 'dddd, MMMM DO YYYY, h:mm:ss a').tz(newTimezone).utc().format('YYYY-MM-DD HH:mm:ss ZZ');
console.log(localTime);
console.log(localTimeInUtc);
console.log(newTime);
console.log(newTimeInUtc);
Here time in UTC is same for localTime and newTime
According to Docs
Maybe you are looking for format2 in the following code.
format1 - creates moment and sets the time-zone (will not update the offset).
format2 - creates the moment with the given timezone.
var date = moment().format("dddd, MMMM D YYYY, h:mm:ss a"), format = "dddd, MMMM D YYYY, h:mm:ss a", timezone = "America/New_York";
var format1 = moment(date,format).tz(timezone);
var format2 = moment.tz(date, format,timezone);
console.log(format1.format(format)+" -> "+format1.format());
console.log(format2.format(format)+" -> "+format2.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.14/moment-timezone-with-data.min.js"></script>
See this Github issue once.
This is a function being used in a ReactJS code base.
Right now, dates that are passed into this function as "null" rather than a string are being rendered as Dec 31, 1969. But no date should be showing if the date is passed in as null.
I want to modify my function below to check for null dates and set those dates to undefined. How should I do that?
var formatTheDate = function(dateString, dateFormat) {
dateFormat = dateFormat || "MMMM DD YYYY, h:mm a";
return moment.utc(new Date(dateString)).local().format(dateFormat);
};
Just check the dateString before attempting to use it.
var formatTheDate = function(dateString, dateFormat) {
dateFormat = dateFormat || "MMMM DD YYYY, h:mm a";
return dateString
? moment.utc(new Date(dateString)).local().format(dateFormat)
: undefined;
};
Just return undefined or null or your choice if the date isn't valid or returns the default date from unix time 0.
var formatTheDate = function(dateString, dateFormat) {
var newDate = new Date(dateString);
//optionally return a value instead of undefined here
if(newDate == "Invalid Date" || newDate - new Date(null) == 0) return;
dateFormat = dateFormat || "MMMM DD YYYY, h:mm a";
return moment.utc(newDate).local().format(dateFormat);
};