简体   繁体   English

如果Java语言中的日期不正确,请更改日期格式

[英]Changing the date format if not correct in Javascript

I have created a mobile app that scans a QR-CODE that has various information embedded within it. 我创建了一个移动应用程序,该应用程序扫描其中嵌入了各种信息的QR码。 One of the bits of information is a date. 信息之一是日期。

There has been changes with in the QR-CODE from the previous versions with the format of the date. 与以前的版本相比,QR码中的日期格式有所更改。 The previous date format was mm/dd and the new version is yy-MM-dd. 以前的日期格式为mm / dd,新版本为yy-MM-dd。

I am able to get it to scan the new version of the labels or the old ones, but not both. 我能够扫描标签的新版本或旧版本,但不能同时扫描两者。 I need to get it to recognize bot types. 我需要获取它来识别机器人类型。 I am not sure if there is a way to use an if statement to recognize the old format and convert it to the new format. 我不确定是否可以使用if语句识别旧格式并将其转换为新格式。

Here is an example of the working code using the new labels. 这是使用新标签的工作代码示例。

(function(){
'use strict';

var app = angular.module('Controllers', []);
var baseUrl = 'https://apps.laticrete.com/LSCWebApiService/lscapi/';

app.controller('BarcodeCtrl', ['$scope', '$state', '$stateParams', '$http', 'alertsManager', '$timeout', 'localStorageService', '$cordovaBarcodeScanner', '$cordovaGeolocation', '$filter',
    function($scope, $state, $stateParams, $http, alertsManager, $timeout, localStorageService, $cordovaBarcodeScanner, $cordovaGeolocation, $filter) {
        var SessionId = localStorageService.get('SessionId');

        // Get GeoLocation
        $cordovaGeolocation
            .getCurrentPosition()
            .then(function(position) {
                $scope.lat = position.coords.latitude;
        $scope.long = position.coords.longitude;
            });

document.addEventListener("deviceready", function() {
    $scope.scanMaterial = function() {


        $cordovaBarcodeScanner
            .scan()
            .then(function(result) {

                var codeArray = result.text.split(',');
                $scope.SKU = codeArray[1].replace("I:", "").trim();
                $scope.ControlNumber = codeArray[0].replace("W:", "").trim(); 
                //$scope.ManufactureDate = codeArray[2].replace("MFG:", "").trim();
                $scope.ManufactureDate = codeArray[2].replace("MFG:", "20").replace(/(\d{4})(\d{2})(\d{2})/, "$1-$2-$3").trim();

                $scope.BatchCode = codeArray[3].replace("B:", "").trim();

                var dataObj = {
                    SessionId: SessionId,
                    JobId: $stateParams.JobId,
                    ManufactureDate: $scope.ManufactureDate, 
                    BatchCode: $scope.BatchCode,
                    SKU: $scope.SKU,
                    ControlNumber: $scope.ControlNumber,
                    CreatedClient: new Date(),
                    Latitude: $scope.lat,
                    Longitude: $scope.long, 
                    Product: { Id : 1}
                };

                $http.post(baseUrl + 'Material/PostNewMaterial', dataObj)
                    .success(function() {
                        alertsManager.addAlert('Success: You have successfully added material.', 'alert-success');
                        $state.go('^');
                    }).error(function(dataObj) {
                        alertsManager.addAlert('Failure Meesage: ' + JSON.stringify({dataObj:dataObj}), 'alert-danger');
                    });
                    $timeout(function(){
                        alertsManager.clearAlerts();
                    }, 5000);

            }, function(error) {
                console.log("An error has happened " + error);
            });
    };
}, false);


}]);

})(); })();

The part of the code I am asking for help on is $scope.ManufactureDate 我要寻求帮助的代码部分是$ scope.ManufactureDate

Thank You in advance. 先感谢您。

Check if the code matches one of the formats and if it does, parse it accordingly. 检查代码是否与格式之一匹配,如果匹配,则进行相应的解析。

if (codeArray[2].match(/MFG:\d+-\d+-\d+/) !== null) {
    /* The format is of 'MFG:000-000-000', \d+ matches 1..n numbers */
    ...
}

Or you could combine these to a one regex: 或者,您可以将这些组合成一个正则表达式:

let matches = codeArray[2].match(/MFG:(20(\d{2})-\d{2}-\d{2})|(\d{2}/\d{2}))/);

See String.prototype.match 参见String.prototype.match

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM