I am using moment from mongorc.js (reference: https://raw.githubusercontent.com/gabrielelana/mongodb-shell-extensions/master/released/mongorc.js) when executed this small snippet its printing “invalid date”
var text = "00000000000000" var text1 = moment(text).format("MM/DD/YYYY, HH:mm") print(text1) moment(text,"YYYYMMDDHHmmss") var text2=moment(text,"YYYYMMDDHHmm").format("MM/DD/YYYY, HH:mm"); print(text2)
But in Java:
import java.text.SimpleDateFormat; public class DateTest { public static void main(String[] s) throws Exception { String a = "00000000000000"; SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm"); System.out.println(format.parse(a)); }}
Prints Sun Nov 30 00:00:00 IST 2
Though may not be correct, I want not “Invalid date” to be printed. Where did I go wrong?
Advertisement
Answer
I have hard coded the date value and it worked:
var text = "00000000000000" if(RegExp(/^0*$/).test(text)) { text = "00010101000000" moment(text,"YYYYMMDDHHmmss").format("MM/DD/YYYY, HH:mm"); }